Generating Random Assoications

time to read 2 min | 354 words

Several times recently I had to created what is basically random assoications between two sets of tables. The problem is usually trying to get some data for the UI. Here is what I came up with:

update Policies

set Status = (select top 1 Id from

      PolicyStatuses where Policies.Id != PolicyStatuses .Id

      order by newid())

The only interesting part is the where clause, since it forces SQL Server to evaluate the statement on a row by row basis. This has horrible performance, by the way.