473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing table name to Stored Procedure

Hi,

I am trying to pass the table name to stored procedure and then use the passing table name in select, update or delete query statement.
When I tried to do that, I got the error such as '@tablename must be declared'.

The example syntax would be like below.

CREATE PROCEDURE procedure_name
@tablename varchar(50)
AS

BEGIN

SELECT *
FROM @tablename

END


Is there anyway I can pass the table name to stored procedure?
Aug 16 '07 #1
3 2902
You can do it pretty much like you've got it set up with the input parameter.

The only thing is you cant execute a select statement directly from your string variable like that.

Instead, build the entire statement where you hope to use that variable into new string variable and then EXEC the whole thing

Example:

Expand|Select|Wrap|Line Numbers
  1. Declare @sqlstr varchar(250)
  2.  
  3. @sqlstr = 'Select * From ' + @tablename
  4.  
  5. (Exec @sqlstr)
  6.  
Aug 17 '07 #2
Thanks.

Actually, I am trying to use the tablename paraemeter with Cursor.
When I tried this:

1. DECLARE @SQLSTR VARCHAR(250),
2. @ID INT
3. SET @SQLSTR = 'SELECT ID FROM ' + @tablename

4. DECLARE MyCursor FOR (Exec (@SQLSTR))
5. OPEN MyCursor
6. FETCH MyCursor INTO @ID

I got the error message :: Incorrect syntax near the keyword 'FOR'

how can I fix the error?
Aug 17 '07 #3
azimmer
200 Expert 100+
Look at this: http://www.thescripts.com/forum/thread686090.html
Aug 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
1
by: Roman Prigozhin | last post by:
Hi all, I have one stored procedure where I defined 3 temporary tables, which i return back to java. I want to have 3 separate sub procedures which would go after the data and fill out these...
6
by: dharmadam | last post by:
Is it possible to pass a column name or the order of the column name in the DB2 table table function. For example, I want to update the address of a person by passing one of the address column name...
8
by: Viator | last post by:
Hi All; I am working on project; where I need to call a DB2 stored procedure (also to be written in the project) which will update/insert some records in the database. The number of rows to be...
15
by: Steve | last post by:
I am trying to develop a stored procedure for an existing application that has data stored in numerous tables, each with the same set of columns. The main columns are Time and Value. There are...
1
by: jkeel | last post by:
If I try to Update a record with the following code using a stored procedure I get an error: <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$...
1
by: chariclark | last post by:
This may be a quick fix post... ---------------------------- I am having trouble passing multiple values into stored procedure. Here it is below: CREATE Procedure spGetAssociateds ( @PDSI...
2
by: Krishbiztech | last post by:
hi all I have developed an asp.net application that handles college operations.. and i have created a stored procedure to delete the records under some constraints.. I have passed table...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.