Ayende @ Rahien

Unnatural acts on source code

Generating Random Assoications

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.

Comments

Chris Khoo
05/25/2007 02:06 AM by
Chris Khoo

Man, that's a neat throwaway one-liner.

Chris

Comments have been closed on this topic.