Vijay Yadav

Vijay Yadav

  • NA
  • 1.4k
  • 652.3k

Random number generation in sql

Jul 4 2011 10:46 AM
I want employees id to be fetched randomly by using the blow query

select Top 2 SessionId from Flash_Temp2
order by NEWID()

but instead of top 2, i want to pass it in a variable as its depend on the situation  for ex:

declare @id int
set @id=3
select Top @id SessionId from Flash_Temp2
order by NEWID()

it givs following error:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '@id'.


Answers (12)