473,507 Members | 6,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Table Name in a Stored Procedure

Hello Forum !

I want to have the tablename "dbo.Enbxxxx" as an additional parameter
for a procedure like this:

ALTER Procedure prcSucheUNR
(
@UNR int
)
As
set nocount on

SELECT ABRUFNR,
UNR,
STICHTAG,
Datum,
Inhalt
INTO #temp FROM dbo.Enb WHERE UNR = @UNR

For some reason:

@tablename varchar(11),

and: INTO #temp FROM @tablename WHERE .... does not work. :-(

I get the following syntax Error:
Zeile 33: Falsche Syntax in der Nähe von '@tablename'.

I'am sure it is possible but i don't know how.

Greetings
Jul 20 '05 #1
3 20341
MC
You need to build and execute dynamic string. Something like:
execute 'SELECT *
INTO #temp
FROM '+@tablename+'
WHERE .... '
However, I would recommend visiting
http://www.sommarskog.se/dynamic_sql.html and reading about it before going
further...

MC
"Rudi" <fn**********@aareon.com> wrote in message
news:5d**************************@posting.google.c om...
Hello Forum !

I want to have the tablename "dbo.Enbxxxx" as an additional parameter
for a procedure like this:

ALTER Procedure prcSucheUNR
(
@UNR int
)
As
set nocount on

SELECT ABRUFNR,
UNR,
STICHTAG,
Datum,
Inhalt
INTO #temp FROM dbo.Enb WHERE UNR = @UNR

For some reason:

@tablename varchar(11),

and: INTO #temp FROM @tablename WHERE .... does not work. :-(

I get the following syntax Error:
Zeile 33: Falsche Syntax in der Nähe von '@tablename'.

I'am sure it is possible but i don't know how.

Greetings

Jul 20 '05 #2
See http://www.sommarskog.se/dynamic_sql.html for a discussion of dynamic
SQL.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Rudi" <fn**********@aareon.com> wrote in message
news:5d**************************@posting.google.c om...
Hello Forum !

I want to have the tablename "dbo.Enbxxxx" as an additional parameter
for a procedure like this:

ALTER Procedure prcSucheUNR
(
@UNR int
)
As
set nocount on

SELECT ABRUFNR,
UNR,
STICHTAG,
Datum,
Inhalt
INTO #temp FROM dbo.Enb WHERE UNR = @UNR

For some reason:

@tablename varchar(11),

and: INTO #temp FROM @tablename WHERE .... does not work. :-(

I get the following syntax Error:
Zeile 33: Falsche Syntax in der Nähe von '@tablename'.

I'am sure it is possible but i don't know how.

Greetings

Jul 20 '05 #3
Hello all !

This is what i was always looking for (and never found)
http://www.sommarskog.se/dynamic_sql.html

My problem is no longer a problem. :-)

Thank you for the quick response
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4947
by: Robin Tucker | last post by:
Hi, I'm trying to determine with my program whether or not a given database supports a given feature set. To do this I'm querying for certain stored procedures in the sysobjects table and if...
10
5586
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...
0
1635
by: joe pribele | last post by:
I have this stored procedure that takes some xml as input. What I need to is use xml as a table so that I can join other tables to the xml and get information back that matches the criteria. I...
1
3672
by: Lauren Quantrell | last post by:
I have read the newsgroups and see this is a common issue but I saw no resolution for it: I have an Access2K frotn end and SQL Server 2K backend. In access, I create a temp table using code in a...
6
26808
by: antmail | last post by:
Hi guys, I have spend several days now trying to solve this problem inside a stored procedure. I want to call a procedure providing 3 variables. The variables area used to select the appropriate...
1
6838
by: ishakteyran | last post by:
hi all; i am using a db2 database on RAD platform and need to generate tables (names of them will be determined by users.) so i thought i can do this with a stored procedure which takes the table...
0
4666
by: amit | last post by:
The Table tt_local will be created when you execute the procedure. The table does not exist when you compile it. There are two options 1. Create the global temporary table manually Remove the...
3
2832
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be...
9
3832
by: Slickuser | last post by:
Hi, I'm trying to use my variable in another variable. How can I achieve that? I can use + (concat) but it will be a lot me to concatenate. I have to use 3 variables with 15 replacement. ...
3
7690
by: dmorand | last post by:
I'm very new to stored procedures so this is probably something very dumb. I want to pass a name of a table to be created to my stored procedure. I'm using the variable @tableName in the code below...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
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...
1
7030
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.