Solved

EXEC DECLARE CURSOR with variable as tablename

Posted on 2004-09-10
3
551 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
  • 3
3 Comments
 
LVL 18

Accepted Solution

by:
ShogunWade earned 50 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

786 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