Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Store Procedure

Author  Topic 

dodi
Starting Member

9 Posts

Posted - 2004-02-04 : 11:37:34
Hi There

Question: In a stored procedure I would like to build the table name from the parameters passed in so the name is dynamic. Then I need to do an insert or update on that table. Are there some examples available?

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-02-04 : 12:26:28
Is something like this what you are looking for?

--***************************************************************

CREATE PROCEDURE DitchProc @TableName VARCHAR(100)
AS
DECLARE @CreateTableSQL VARCHAR(1000)
DECLARE @InsertSQL VARCHAR(1000)
DECLARE @UpdateSQL VARCHAR(1000)

SET @CreateTableSQL = 'CREATE TABLE ' + @TableName + '(Col1 Decimal(18, 2), Col2 Decimal(18,2), SumOfThe2 Decimal(18, 2))'

EXEC(@CreateTableSQL)

SET @InsertSQL = 'INSERT INTO ' + @TableName + ' SELECT rand() * 12345.67, rand() * 98765.43, 0'

EXEC(@InsertSQL)

SET @UpdateSQL = 'UPDATE ' + @TableName + ' SET SumOfThe2 = Col1 + Col2 '

EXEC(@UpdateSQL)

--********************************************************************************

Regards,
Ditch
Go to Top of Page
   

- Advertisement -