Q:

Conditional SQL targeting different servers

How can I have a single piece of SQL conditionally change the target server ? 
So on my local dev machine, I can run it and have the command ...
INSERT  INTO [sds].[dbo].[Team_Email_Addresses]
        SELECT  [class] AS 'Team number' ,
                [description] AS 'Team name' ,
                [DutyEmail] AS 'Team email address'
        FROM    [DEVSERVER].[cases].[dbo].[sp_class]
        WHERE   [status] = 1

But on the test machine I want the same piece of SQL to run and have the command ...
INSERT  INTO [sds].[dbo].[Team_Email_Addresses]
        SELECT  [class] AS 'Team number' ,
                [description] AS 'Team name' ,
                [DutyEmail] AS 'Team email address'
        FROM    [TESTSERVER].[cases].[dbo].[sp_class]
        WHERE   [status] = 1

I've tried using a variable @TGT and setting it to either devserver or testserver with the SQL FROM    [@TGT].[cases].[dbo].[sp_class], but I get an error Could not find server '@TGT' in sys.servers.

A:

You would need to use dynamic SQL. Also you should not be using 'single quotes' to delimit column aliases; this syntax has been deprecated. You should be using [square brackets] or "double quotes" (the former generally preferred).
DECLARE @sql NVARCHAR(MAX), @TGT SYSNAME;

SET @TGT = N'[TESTSERVER]';

SET @sql = N'INSERT  INTO [sds].[dbo].[Team_Email_Addresses]
        SELECT  [class] AS [Team number] ,
                [description] AS [Team name] ,
                [DutyEmail] AS [Team email address]
        FROM    ' + @TGT + '.[cases].[dbo].[sp_class]
        WHERE   [status] = 1;';

PRINT @sql;
EXEC sp_executesql @sql;

(In fact you don't really need the aliases here, though you should be specifying the insert column list.)
Or, what you can do to make the code more stable, is to use synonyms in your two environments. In dev:
CREATE SYNONYM dbo.sp_class FOR DEVSERVER.cases.dbo.sp_class;

In Test:
CREATE SYNONYM dbo.sp_class FOR TESTSERVER.cases.dbo.sp_class;

Now your query can simply be:
INSERT  INTO [sds].[dbo].[Team_Email_Addresses]
        SELECT  [class],
                [description],
                [DutyEmail]
        FROM    [dbo].[sp_class]
        WHERE   [status] = 1;

Of course synonyms only work if you are on SQL Server 2005 or better. It is always useful to specify the version of SQL Server you are using!

