Solved

Creating a variable which can store tablename

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore 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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

18 Experts available now in Live!

Get 1:1 Help Now