Solved

Table Names as Parameters for Stored Procedures

Posted on 1998-08-07
6
204 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

617 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