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'.
Loading


VulpesRank #55 · 29.2K reputationPosted Jul 4, 2011, 6:58 AM EDT
Vijay YadavRank #1,637 · 943 reputationPosted Jul 4, 2011, 7:22 AM EDT
Vijay YadavRank #1,637 · 943 reputationPosted Jul 4, 2011, 7:33 AM EDT
One more doubt i want to say all that in 3 different variable i want to know how to do as there is nothing like array that we have in programming language. Any suggestion?
VulpesRank #55 · 29.2K reputationPosted Jul 4, 2011, 8:02 AM EDT
Vijay YadavRank #1,637 · 943 reputationPosted Jul 4, 2011, 8:12 AM EDT
Considering our example,
How to save this in varibale i didn't get it.
Please help
VulpesRank #55 · 29.2K reputationPosted Jul 4, 2011, 8:59 AM EDT
Vijay YadavRank #1,637 · 943 reputationPosted Jul 4, 2011, 9:06 PM EDT
Suthish NairRank #92 · 22.8K reputationPosted Jul 5, 2011, 4:55 AM EDT
Vijay YadavRank #1,637 · 943 reputationPosted Jul 5, 2011, 5:29 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 5, 2011, 6:19 AM EDT
It's therefore just a matter of devising code which assigns the appropriate value to @id before you set the rowcount.
I don't think I (or Suthish) can help you further without knowing how this will be determined in particular circumstances.
Vijay YadavRank #1,637 · 943 reputationPosted Jul 5, 2011, 6:32 AM EDT
Can you please check the below link, may be here i didn't ask the question properly
http://www.c-sharpcorner.com/Forums/Thread/130225/assign-single-varible-a-multiple-rows-using-select-query-in.aspx
Please check it..
VulpesRank #55 · 29.2K reputationPosted Jul 5, 2011, 7:10 AM EDT
The code will be something like this:
declare @id int
declare Employee_Cursor cursor for
select EmpId from Employee
open Employee_Cursor
fetch next from Employee_Cursor into @id
while @@fetch_status = 0
begin
set rowcount @id
select SessionId from Flash_Temp2
order by NEWID()
// do something with SessionId
fetch next from Employee_Cursor into @id
end
close Employee_Cursor
deallocate Employee_Cursor