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

VulpesPosted Jul 4, 2011, 10:58 AM
VulpesPosted Jul 5, 2011, 11:10 AM
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
Vijay YadavPosted Jul 5, 2011, 10:32 AM
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..
VulpesPosted Jul 5, 2011, 10:19 AM
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 YadavPosted Jul 5, 2011, 9:29 AM
Suthish NairPosted Jul 5, 2011, 8:55 AM
Vijay YadavPosted Jul 5, 2011, 1:06 AM
VulpesPosted Jul 4, 2011, 12:59 PM
Vijay YadavPosted Jul 4, 2011, 12:12 PM
Considering our example,
How to save this in varibale i didn't get it.
Please help
VulpesPosted Jul 4, 2011, 12:02 PM
Vijay YadavPosted Jul 4, 2011, 11:33 AM
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?
Vijay YadavPosted Jul 4, 2011, 11:22 AM