Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

variable tablename ?

Posted on 1998-04-07
4
Medium Priority
?
1,327 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
[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
  • 2
4 Comments
 
LVL 9

Accepted Solution

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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

688 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