Solved

variable tablename ?

Posted on 1998-04-07
4
1,291 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
Comment Utility
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
Comment Utility
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
Comment Utility
I would like to grade spirididonov with an AAA.
Thanks a lot !
0
 
LVL 7

Expert Comment

by:spiridonov
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Viewers will learn how the fundamental information of how to create a table.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now