Solved

Pass tablename to stored procedure

Posted on 2002-03-25
2
636 Views
Last Modified: 2012-08-13
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
Comment
Question by:meimeius
2 Comments
 
LVL 6

Accepted Solution

by:
acampoma earned 50 total points
ID: 6894917

try this
declare @SQL Varchar(1000)
Set @SQL='select * from ' + @TableName
exec(@SQL)
0
 

Author Comment

by:meimeius
ID: 6895125
Thank you very much!  Excellent!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 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