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

  • SEARCH
  • Home
  • 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 8330697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:11:49+00:00 2026-06-09T02:11:49+00:00

Can anyone tell me why this doesn’t work? if ((select @@servername) = ‘ServerA’) begin

  • 0

Can anyone tell me why this doesn’t work?

if ((select @@servername) = 'ServerA')
begin
    use DatabaseA
    select top 5 * from dbo.SignUPRequest
end
else if ((select @@servername) = 'ServerB')
begin
    use DatabaseB
    select top 5 * from dbo.SignUPRequest
end

When I run this on ServerA, I get a message that DatabaseB doesn’t exist on ServerA, which it doesnt, but I don’t understand why it’s trying to read if the second if evaluates to false.

Msg 911, Level 16, State 1, Line 8
Database ‘DatabaseB’ does not exist. Make sure that the name is entered correctly.

  • 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-09T02:11:51+00:00Added an answer on June 9, 2026 at 2:11 am

    The code is parsed before it is run. When it is parsed SQL Server checks that it can access everything in the code, it cannot access the database that exists on the other server so the parsing step of running the code fails. As a result, you get the error message you’ve shown.

    If you want to get around that you can put the code in the IF blocks as dynamically executed code (I always feel this is a bit of a hack/workaround).

    DECLARE @sql NVARCHAR(4000);
    if ((select @@servername) = 'ServerA')
    begin
        SET @sql = 'use DatabaseA; select top 5 * from dbo.SignUPRequest;'
    end
    else if ((select @@servername) = 'ServerB')
    begin
        SET @sql = 'use DatabaseB; select top 5 * from dbo.SignUPRequest'
    end
    
    EXEC (@sql)
    

    So, what happens here is that you defer the parsing & running of the code that uses the appropriate database for the server to run time, as that what the EXEC statement at the end does.

    UPDATE

    Based on the additional comment below you could also rewrite this as:

    DECLARE @sql NVARCHAR(4000);
    if ((select @@servername) = 'ServerA')
    begin
        select top 5 * from DatabaseA.dbo.SignUPRequest;
    end
    else if ((select @@servername) = 'ServerB')
    begin
        select top 5 * from DatabaseB.dbo.SignUPRequest;
    end
    

    So, instead of putting in a USE <database-name> at the start, you can also more fully qualify the name of the table in your select statement. If you only have one line of SQL to deal with this could be more effective.

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

Sidebar

Related Questions

I'll keep this brief: can anyone tell me why this doesn't work? The else
Can anyone please tell why this code doesn't work: $ser = array('one','two','three','four'); $x =
Can anyone tell why this doesn't work? http://www.webdevout.net/test?01x afaik it should; my <div> elements
can anyone tell me why this doesn't work? db = openOrCreateDatabase(database.db, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault());
Can anyone tell me why this doesn't work? <?php $lang = $_get[lang]; if (($lang
Can anyone tell me why this doesn't work? var leftPos = (10 * 20)+
Can anyone tell me why this doesn't work? jeMarkers is an array of Google
Can anyone tell me why this code doesn't work in an 2011 online plugin?
This doesn't work. Can anyone tell why? #include <iostream> using namespace std; int mean(
Could anyone tell me why this code doesn't work? I can't even get the

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.