Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Question about Store Procedure

Author  Topic 

Mazdak
Yak Posting Veteran

63 Posts

Posted - 2003-02-06 : 08:07:41
I want to write a store procedure that SELECT from a table.I want to pass the name of table to store procedure as an argument.I write this code:

CREATE PROCEDURE sp_Vote_GetVoteArray
@Table nvarchar(50),
@ID int
AS

SELECT col1 FROM @Table WHERE id=@ID



But it gives me error that @Table variable must declare!

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2003-02-06 : 10:28:07
Seems like you are using SQL 2000, which supports table variables, but it doesn't look like that's what you want here. If you are trying to have a dynamic Select over any table, you need to use dynamic sql and change your query to look like this:
EXEC('SELECT Col1 FROM ' + @Table + ' WHERE ID = ' + @ID)
or put your select string into a string variable like this:
Declare @strSelect varchar(250)
set @StrSelect = 'SELECT Col1 FROM' + @Table + ' WHERE ID = ' + @ID
EXEC @strSelect

Sarah Berger MCSD
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-02-06 : 16:03:01
You'll lose the benfits of the optimizer with dynamic sql...so why not go crazy!!

Check out this link:

http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=23604

Good Luck

Brett

8-)

Go to Top of Page
   

- Advertisement -