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 2005 Forums
 Transact-SQL (2005)
 Table Name as Parameter In Stored Procedure

Author  Topic 

SQLSeeker
Starting Member

15 Posts

Posted - 2009-11-05 : 00:45:38
Hi,
I want to send table name as parameter to the Stored procedure so that I can do database operation on a table which is in parameter list. How it can be achieved??

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-11-05 : 00:51:10
Try like this,

/*

exec sample 'mytable'

*/

Create procedure sample @table_name varchar(50)
as
Begin

exec('select * from '+@table_name)

End


Go through Dynamic SQL in BOL


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-11-05 : 00:56:21
The thing is like when you going to pass database name or table name using a parameter then you have to use dynamic sql to execute this thing.

iF theRe iS a wAy iN tHen theRe iS a wAy oUt..
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-05 : 01:34:09
There is no point in using stored procedures if you are going to use code like that. You might as well use inline SQL in your application.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -