SQLTeam.com | Weblogs | Forums

Dynamic Table Name


#1

Hi,

Using SQL 2008 R2

i would create a TSQL SQLCMD Script for Create Storage Procedure. In One SP i have something like:

DELETE FROM dbo.[TableCompanyNameTestVersion$Change Log Entry]

in the Live SQL Version i must change this to:

DELETE FROM dbo.[TableCompanyNameLiveVersion$Change Log Entry]

How can i make this dynamic? So i can use the Script in both Databases?
Or Must I Set this in a Variable ? Like:

:SETVAR TableCompanyName "MyTableName" 

Understand you what i mean? (And Sorry my bad english)
Thanks.

Pit


#2

You could use something like:

IF @@SERVERNAME = 'myserver1' 
BEGIN
DELETE FROM dbo.[TableCompanyNameLiveVersion$Change Log Entry]
END
ELSE 
BEGIN
DELETE FROM dbo.[TableCompanyNameTestVersion$Change Log Entry]
END