Solved

variable tablename ?

Posted on 1998-04-07
4
1,320 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 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

615 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