Solved

EXEC DECLARE CURSOR with variable as tablename

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server Log File Space 6 34
t-sql complement 8 32
Recurring Excel Timelime for Veeam 2 34
Sql query to Stored Procedure 6 16
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 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

22 Experts available now in Live!

Get 1:1 Help Now