Solved

Table Names as Parameters for Stored Procedures

Posted on 1998-08-07
6
195 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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…
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.
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 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