Solved

Creating a variable which can store tablename

Posted on 2008-06-19
3
488 Views
Last Modified: 2008-11-21
I have a stored procedure in which I want to create a cursor. The tablename from which the cursor will fetch data will be decided in the stored procedure based on some condition. So I want to store the tablename in some variable. I created a variable of type 'sysname' in which the tablename was assigned. But when I used that variable in the declare cursor statement as:
declare cur cursor for select * from @tabname    --where @tabname is of type sysname
the following error arose:
"Must declare scalar variable @tabname"
Please help me to solve this error.
0
Comment
Question by:ShreyaTrivedi
[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 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 21820646
Hello ShreyaTrivedi,

exec ('declare cur cursor for select * from '+ @tabname   )

GOD Bless,
Aneesh R.
0
 

Author Comment

by:ShreyaTrivedi
ID: 21828697
If I declare cursor in this way then I am not able to access the cursor in the section follwing the declare statement. ie. if after declaring  the cursor in this way , if I write 'open cur' it produces an error that cur is not declared.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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