Consider this data:
CREATE TABLE
I want to set the Prefix value in #Data as a random Prefix from #Prefix with the appropriate Code .
Using the inner join direct just results in using a single value:
UPDATE D SET Prefix = P.Prefix FROM
From reading other questions here, NEWID() recommended as a way to randomly arrange something. Change join to:
SELECT TOP 1 subquery ordering by NEWID()
still selects only one value (albeit random each time) for each row:
UPDATE D SET Prefix = (SELECT TOP 1 P.Prefix FROM
So I'm not sure how to get a random prefix for each data record from one update statement. I could probably do some kind of loop through the #Data table, but I never touch loops in SQL , and I'm sure this will be slow. The actual application of this will be on tens of thousands of entries, with hundreds of prefixes for dozens of codes.
source share