Solved

Table Names as Parameters for Stored Procedures

Posted on 1998-08-07
6
198 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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