I need to write a stored procedure that selects from a table and
returns the result set. I don't always know the TableSchema that I
need to use when qualifying the table at run-time
Example: The correct table could either be dbo.MyTable or
zzz.MyTable.
I want the user to enter the name of the schema as a parameter of the
procedure at run-time.
However, SQL Server gives me an error when I try create the procedure
using the parameter in the FROM clause.
Can anyone give me a hand?
Thanks,
Bill
Run the scripts below to see my problem:
/* Make the sample table */
create table MyTable (TabKey int NOT NULL);
alter table MyTable add constraint MyTable_PK primary key (TabKey);
/* insert sample values */
insert into MyTable values (1);
insert into MyTable values (2);
insert into MyTable values (3);
/*This statement works fine. (Notice, I don't make use of @TableSchema
just declare it to prove there is
no syntax error in the declaration */
CREATE PROCEDURE TestProc (@TableSchema varchar(80)) AS
BEGIN
SELECT * from dbo.MyTable
END;
/* Run the Procedure (Doesn't matter what I put for the parameter) */
TestProc 'dbo'
/* Drop Procedure */
drop procedure TestProc
/* Try to Re-create the procedure with a parameterized FROM clause
that uses @TableSchema, but get an error */
CREATE PROCEDURE TestProc (@TableSchema varchar(80)) AS
BEGIN
SELECT * from @TableSchema.My Table ---- <<<< This causes the
error
END
/*
Here is the error message:
Msg 156, Level 15, State 1, Procedure TestProc, Line 5
Incorrect syntax near the keyword 'END'.
*/
returns the result set. I don't always know the TableSchema that I
need to use when qualifying the table at run-time
Example: The correct table could either be dbo.MyTable or
zzz.MyTable.
I want the user to enter the name of the schema as a parameter of the
procedure at run-time.
However, SQL Server gives me an error when I try create the procedure
using the parameter in the FROM clause.
Can anyone give me a hand?
Thanks,
Bill
Run the scripts below to see my problem:
/* Make the sample table */
create table MyTable (TabKey int NOT NULL);
alter table MyTable add constraint MyTable_PK primary key (TabKey);
/* insert sample values */
insert into MyTable values (1);
insert into MyTable values (2);
insert into MyTable values (3);
/*This statement works fine. (Notice, I don't make use of @TableSchema
just declare it to prove there is
no syntax error in the declaration */
CREATE PROCEDURE TestProc (@TableSchema varchar(80)) AS
BEGIN
SELECT * from dbo.MyTable
END;
/* Run the Procedure (Doesn't matter what I put for the parameter) */
TestProc 'dbo'
/* Drop Procedure */
drop procedure TestProc
/* Try to Re-create the procedure with a parameterized FROM clause
that uses @TableSchema, but get an error */
CREATE PROCEDURE TestProc (@TableSchema varchar(80)) AS
BEGIN
SELECT * from @TableSchema.My Table ---- <<<< This causes the
error
END
/*
Here is the error message:
Msg 156, Level 15, State 1, Procedure TestProc, Line 5
Incorrect syntax near the keyword 'END'.
*/
Comment