Solved

EXEC DECLARE CURSOR with variable as tablename

Posted on 2004-09-10
3
549 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now