[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

Pass tablename to stored procedure

I have a stored procedure called upsTest(see below).  I’d like to pass table name as a parameter, so I create another stored procedure called uspTest1, but it doesn’t work.  Is there any way I can use stored procedure to pass tablename?   Thanks in advance.

CREATE PROCEDURE uspTest  
AS
select * from table1


CREATE PROCEDURE uspTest1  
@TableName varchar(50)
AS
select * from @TableName
0
meimeius
Asked:
meimeius
1 Solution
 
acampomaCommented:

try this
declare @SQL Varchar(1000)
Set @SQL='select * from ' + @TableName
exec(@SQL)
0
 
meimeiusAuthor Commented:
Thank you very much!  Excellent!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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