Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7895957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:46:11+00:00 2026-06-03T07:46:11+00:00

How can I have a single piece of SQL conditionally change the target server

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:46:12+00:00Added an answer on June 3, 2026 at 7:46 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you have a single SQL that order events by current day then future
I have a single table in the Sql Server 2008 r2 DB. Every few
I realized that I can have problems with single quotes in php arrays: <?php
i have a single textbox named Keywords. User can enter multiple strings for search.
I have a single annotation on a map view. I can select it programmaticly,
The javascript code I now have can toggle a single row of data. Whether
I have a situation in which user can single tap a control, which show
Can you recommend a single downloadable video ( one that you have actually watched
I have a string like this BRADI5G20430|BRADI5G20430.1||1 How can I replace the bar (single
How can I store arrays in single array? e.g. I have four different arrays,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.