Different Database Names

  • On my development machine, the database I want to query is called DataBase_A.  On the production machine, that same database is called DataBase_B.  So If I want to select data from DataBase_A, I use query #1.  When I move the stored procdure to the production machine, I need to rename the database references in the stored procedure.

    Query #1:

    SELECT *

    FROM DataBase_A..Table1

    Query #2:

    SELECT *

    FROM DataBase_B..Table1

    So I try this and of course it doesn't work.

    DECLARE @DataBaseName varchar(10)

    SELECT @DataBaseName = 'DataBase_A'

    SELECT *

    FROM @DataBaseName..Table1

    Is there a way to do this in a single stored procedure?

    Is there a way to do this across the entire database?  Thanks!

  • Hi,

    I think you could try this;

    DECLARE @DataBaseName varchar(10)

    SELECT @DataBaseName = 'DataBase_A'

    EXEC ('SELECT * FROM ' + @DataBaseName + '..tablename')

     

    You could make the table a variable as well.

    Hope this helps.

    Jon

  • Why not try something like

    IF @@SERVERNAME = 'Me'

      BEGIN

        SELECT * FROM DataBase_A..Table1

      END

    ELSE

      BEGIN

        SELECT * FROM DataBase_B..Table1

      END



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply