tsql - Using a cursor with dynamic SQL in a stored procedure -


I have a dynamic SQL statement that I created in a stored procedure. I use the cursor again to get results Need to run I have difficulty finding the right syntax here What I'm doing here Perform

  select @SQLStatement = declared Choose userId from users' @UserId users_cursor cursor announced here @SQLStatment --Fails for. Like Doesn''t fetch the open users_cursor @UserId inquiry Next users_cursor while @@ FETCH_STATUS = 0 start the completion of the closing EXEC asp_DoSomethingStoredProc @UserId users_cursor deallocate users_cursor  

Right to What's the way

declared

A cursor will accept only one select statement, so if the SQL is really dynamic You need to use the global cursor to work your server down the cursor part of the statement you are performing. Of

  @UserID varchar (100) @sqlstatement nvarchar (4000) announced to declare --move declared cursor to execute announced cursor SQL set @sqlstatement = 'User to User to select the cursor "User exec from Sp_executesql @sqlstatement open users_cursor fETCH next users_cursor from @UserId WHILE @@ FETCH_STATUS = Print 0 user IDE eXEC asp_DoSomethingStoredProc @UserId fETCH next users_cursor - completion of @UserId closing speed again Users_cursor re-allocation users_cursor within the loop check  

If the world If you need to avoid using the cursor, you can also put the results of your dynamic SQL in a floating table, and then use that table to fill your cursor.

  @UserID varchar (100) Table #users make (UserID varchar (100)) @sqlstatement nvarchar (4000) announcing declared Insert set @sqlstatement = '#users (User ID) Users 'F (@ Skyuelstetment) select user D from Iclaijh the Ugrcars cursor Users_cursor cursor # Open Ugrcars Fchet from user @ Next userid @ in Ugrsakskars before users_cursor FETCH_STATUS = 0 Aksipi Apiseepi Apeedioacastasted Pro Uaraiaidiaidi Foike ahead Users in User Aid UserUserDirectDellector UserCaror Drop Table #User  / Pre> 

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -