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 8067527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:19:35+00:00 2026-06-05T12:19:35+00:00

Trying to have a single SQL script that works on a number of environments,

  • 0

Trying to have a single SQL script that works on a number of environments, but the USE statement causes an error for the other environment, i.e. in the example below, when running against SQL02, the USE [application], even when inside a BEGIN ... END?

-- connected to SQL02
DECLARE @STAGE INTEGER = 3
IF @@SERVERNAME = 'SQL02' 
    SET @STAGE = 1
IF @@SERVERNAME = 'SQL03' 
    SET @STAGE = 2

IF @STAGE = 1 
    BEGIN
        IF EXISTS ( SELECT  *
                    FROM    [application_tst].[sys].[database_principals]
                    WHERE   name = N'qauser' ) 
            BEGIN
                USE [application_tst]
                --rest of code
            END
    END

IF @STAGE = 2 
    BEGIN
        IF EXISTS ( SELECT  *
                    FROM    [application].[sys].[database_principals]
                    WHERE   name = N'qauser' ) 
            BEGIN
                USE [application]
                --rest of code
            END
    END
  • 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-05T12:19:36+00:00Added an answer on June 5, 2026 at 12:19 pm

    You will need to use explicit three part names everywhere and not use USE. USE is an execution time command and your batch will fail to compile as expected because you expect the name resolution to occur as if the USE was executed during compilation.

    -- connected to SQL02
    DECLARE @STAGE INTEGER = 3
    IF @@SERVERNAME = 'SQL02' 
        SET @STAGE = 1
    IF @@SERVERNAME = 'SQL03' 
        SET @STAGE = 2
    
    IF @STAGE = 1 
        BEGIN
            IF EXISTS ( SELECT  *
                        FROM    [application_tst].[sys].[database_principals]
                        WHERE   name = N'qauser' ) 
                BEGIN
                    select from [application_tst].dbo.table
                    insert into [application_tst].dbo.table
                    --rest of code, using explicit 3 part names
                END
        END
    
    IF @STAGE = 2 
        BEGIN
            IF EXISTS ( SELECT  *
                        FROM    [application].[sys].[database_principals]
                        WHERE   name = N'qauser' ) 
                BEGIN
                    select from [application].dbo.table
                    insert into [application].dbo.table
                    --rest of code, using explicit 3 part names
                END
        END
    

    An alternative is to use dynamic sql, which will delay compilation until is invoked.

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

Sidebar

Related Questions

I am trying to do multiple counts in a single sql statement. I have
Trying to have my PHP script return some SQL table queries. Here's my script
I'm trying to figure out a single sql statement to remove duplicate records from
I'm trying to construct my linq-to-sql expression so that it only generates a single
i have a single powerpoint slide that we use for reporting. This slidehas some
I trying to make a sql script that will randomize the city, state, and
I am trying to have a label display single-digit numbers as double-digit numbers (ie.
I am trying to have a running average column in the SELECT statement based
I'm trying to have a script automatically transform an xml file into several html
I am trying to have a tree display custom data that looks like this.

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.