Solved

Table Names as Parameters for Stored Procedures

Posted on 1998-08-07
6
190 Views
Last Modified: 2010-03-19
   How do I pass a table name as a parameter to a stored procedure? When I use variables passed as parameters in most contexts, I have no problem(i.e. Select * from Titles Where L_Name Like @lastName). But I can't find a way to use variables in the 'From' clause(i.e. Select * from @tableName).
   I've been able to build Sql strings and execute them using the 'exec' command, but with this approach I'm unable to use the results of the dynamically executed query in the continuation of my stored procedure.
 
0
Comment
Question by:tgoglia
  • 4
  • 2
6 Comments
 

Author Comment

by:tgoglia
ID: 1089452
Edited text of question
0
 
LVL 4

Expert Comment

by:mitek
ID: 1089453
Short answer: EXEC is the only way. You can't do something like
SELECT * FROM @table_name (i wish it were possible)

Yet, you CAN get results from EXEC. That's just nontrivial.

BTW, i wouldn't call it an easy question ...

0
 
LVL 4

Expert Comment

by:mitek
ID: 1089454
Short answer: EXEC is the only way. You can't do something like
SELECT * FROM @table_name (i wish it were possible)

Yet, you CAN get results from EXEC. That's just nontrivial.

BTW, i wouldn't call it an easy question ...

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Accepted Solution

by:
mitek earned 50 total points
ID: 1089455
Try this:

DECLARE @01 varchar(255)
CREATE TABLE #temp(sid int,fname varchar(10))
SELECT @01 = 'SELECT 1,''TEST''' -- dynamic SELECT statement
INSERT INTO #temp EXEC(@01) -- will work for MS SQL 6.5 and higher
SELECT * FROM #temp
DROP TABLE #temp -- optional. the temp table will be dropped automatically when the sp finishes

That's one of very few ways I know of (in MS SQL) to do the thing you are talking about. I doubt that there are lots of other ways to do that ...
0
 

Author Comment

by:tgoglia
ID: 1089456
Thanks for the help. I was thinking of that as a possible solution, but it seemed like such a complicated solution to a problem that I thought was simple. I'm new to the bulletin board/newsgroup thing, so sorry if I underrated my question. Is there a remedy?
0
 
LVL 4

Expert Comment

by:mitek
ID: 1089457
Never mind :)
I would have answered it even if it was only 10 points ...
Besides, it didn't take much time to answer it :)
 
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now