• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

Creating a variable which can store tablename

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
ShreyaTrivedi
Asked:
ShreyaTrivedi
1 Solution
 
Aneesh RetnakaranDatabase AdministratorCommented:
Hello ShreyaTrivedi,

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

GOD Bless,
Aneesh R.
0
 
ShreyaTrivediAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now