?
Solved

Table Names as Parameters for Stored Procedures

Posted on 1998-08-07
6
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 4

Accepted Solution

by:
mitek earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

719 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