?
Solved

EXEC DECLARE CURSOR with variable as tablename

Posted on 2004-09-10
3
Medium Priority
?
565 Views
Last Modified: 2009-12-16
Following code is not working and return A cursor with the name 'Cur' does not exist.
But if i replace EXEC() by PRINT and run the printed line manually is working. What can i do?

EXEC('DECLARE Cur CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR SELECT DISTINCT(UI.FK_UserId), UI.DateInscrit, SM.DateEncaissement FROM Stats..UserNfo UI, SalesMembership SM, Stats_Panier_Moyen_'+@sPays+'_Cumul SPMCC WHERE UI.FK_UserId=SM.FK_UserId AND UI.FK_UserId=SPMCC.FK_UserId AND SM.Annule=0 AND SM.MontantEncaissement>0 AND SM.DateEncaissement<='''+@DateArretSuivi+''' ORDER BY SM.DateEncaissement')

0
Comment
Question by:mammouth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 18

Accepted Solution

by:
ShogunWade earned 200 total points
ID: 12026810
simple answer is that as it is dynamic sql it is creating a local cursor in its own context not that of the caller.

remove the keyword local.  or better still .. dont use dynamic sql.  or even better dont use cursors or dynamic sql.  

twhat is it you are trying to do with the cursor ?
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12026828
oops almost an accidental sear word idue to typo  replace twhat   with what
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12026839
:( my fingers arent working properly today... Ill blame the keyboard :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question