Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedures 1

Status
Not open for further replies.

gillesnebel

Programmer
Mar 22, 2005
13
0
0
CH
Hi,

Can somebody help me with the following stored procedure. I want to declare dynamically the name of table that has to be filled. But there must be a syntax error.....


CREATE PROCEDURE [sp_TransferTax]
(
@DataTableName VarChar (150)
)

AS
INSERT INTO @DataTableName (x,y,z) VALUES (1,2,3)
GO

Can somebody help me?

Thanks
 
You can use dynamic SQL to accomplish this. If there is a relatively small number of tables, I would advise creating a seperate stored procedure for each table.

The dynamic SQL code...

Code:
CREATE PROCEDURE [sp_TransferTax]
(
@DataTableName VarChar (150)
)

AS 
Declare @SQL VarChar(8000)
Set @SQL = 'INSERT INTO ' + @DataTableName + ' (x,y,z) VALUES (1,2,3)'
Exec (@SQL)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
You must use dynamic sql in this situation

Declare @sql nvarchar(500) -- declare variable to hold code

set @sql = "INSERT INTO ' + @DataTableName + '(x,y,z) VALUES (1,2,3)' -- this creates a string which contains your code

sp_executeSql @sql -- this executes your code

 
Yes. I'm going.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks to all replies,... I will try to add some dynamic sql instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top