sql server - T-SQL setting int on a column based on random p-key -
i created following script below. pretty looking update colomn called "c1int" number 1234567 randomly based on pkey of row.
i created random generator pkey uses 1 min , total rows max.
then there loop should update rows on , on based on number in while statement. when run it, updates 1 random row 1234567 number , though still running loop, never updates else. missing something? there better way this?
declare @a int declare @maxpkey int declare @minpkey int declare @randompkey int set @a = 1 set @maxpkey = (select count(*) [loadtesttwo].[dbo].[actbenchdb.table1]) set @minpkey = 1 set @randompkey = round(((@maxpkey - @minpkey -1) * rand() + @minpkey),0) while @a < 500000000000000000000 begin update [loadtesttwo].[dbo].[actbenchdb.table1] set c1int = (1234567) pkey = @randompkey set @a = @a + 1 end
your current loop updates single row because set random key outside loop , run same update statement 500,000,000,000,000,000,000 times (which assuming 1 million executions per second still take 15 million years complete, , hit records anyway).
it clear trying do, don't know why want randomly change data in database. anyway, may not understand why, can @ least how, if want update n random rows, rather running loop n times, better perform single update:
declare @n int = 100; -- number of random rows update update t set cint = 12345467 ( select top (@n) * [loadtesttwo].[dbo].[actbenchdb.table1] order newid() ) t;
Comments
Post a Comment