Solved

variable tablename ?

Posted on 1998-04-07
4
1,302 Views
Last Modified: 2008-08-14
I execute a stored procedure out of VB4.
In this stored procedure there is a SELECT FROM statement.
The problem is that I wanna use the sp to select from different tables. So there is my question:

Can I supply the tablename as a varible?
for example:

sp_get
@tablename char(25)

SELECT * FROM @tablename
0
Comment
Question by:tell
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
cymbolic earned 100 total points
ID: 1091104
No you can't use a parameter for the table name.  A stored procedure is compiled to present an optimized query plan, based upon the tables involved and the columns access/filtered.

The best you can do is to create multiple differently named stored procedures (name them based upon table target table name), and then use a variable name to substitute the procedure name that works with the table you desire at run time.
0
 
LVL 7

Expert Comment

by:spiridonov
ID: 1091105
Yes you can use tablename as a parameter.

CREATE PROCEDURE testt @tabname varchar(30)
AS
DECLARE @sql VARCHAR(100)
SELECT @sql="SELECT * FROM "+@tabname
EXEC(@sql)

0
 

Author Comment

by:tell
ID: 1091106
I would like to grade spirididonov with an AAA.
Thanks a lot !
0
 
LVL 7

Expert Comment

by:spiridonov
ID: 1091107
If you want to grade my answer you should have rejected the proposed answer. Or otherwise post the points as another question

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

Title # Comments Views Activity
Trying to get a Linked Server to Oracle DB working 21 60
Attaching Database Failed ? 3 43
Sql server insert 13 29
string fuctions 4 26
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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