Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

EXEC DECLARE CURSOR with variable as tablename

Posted on 2004-09-10
3
Medium Priority
?
569 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

604 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