Solved

Creating a variable which can store tablename

Posted on 2008-06-19
3
487 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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