Solved

Creating a variable which can store tablename

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

947 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

21 Experts available now in Live!

Get 1:1 Help Now