Dynamically change database depending on the server?

  • We have 2 servers:

    - SRV-LIVESQL with database PRODUCTION

    - SRV-DEVSQL with database DEVELOPMENT

    We have 300+ scripts that we need to manually deploy to both the production and the development servers.

    We need to do it manually, because we need to change some things per feed (e.g. customernumbers, new settings etc..)

    We need to deploy (run) these scripts always both in the DEV and the LIVE version (for consistency purposes)

    What I want is to NOT have to manually change my database depending on the server name, so I was hoping to do something like this:

    DECLARE @SN VARCHAR(255)

    SET @SN = @@SERVERNAME

    IF @SN = 'SRV-DEVSQL' BEGIN

    USE Development

    END ELSE BEGIN

    USE Production

    END

    GO

    This doesn't work because the parser claims that either the Development database, or the Production database doesn't exist:

    Msg 911, Level 16, State 1, Line 7

    Database 'Production' does not exist. Make sure that the name is entered correctly.

    So I changed it to dynamic SQL, basically the same but now something like this:

    DECLARE @SN VARCHAR(255)

    DECLARE @SQL VARCHAR(400)

    SET @SN = @@SERVERNAME

    IF @SN = 'SRV-DEVSQL' BEGIN

    SET @SQL = 'Use Development' + CHAR(13) + 'GO'

    END ELSE BEGIN

    SET @SQL = 'Use Production' + CHAR(13) + 'GO'

    END

    EXEC (@SQL)

    GO

    This is something the parser liked, and it runs.

    Except it doesn't do anything.

    Does anyone know how I can make it so that when I run my query, the query dynamically chooses (and runs on) the correct database depending on the servername?

    Because we do daily work with these queries (they are actually a big bunch of XML feeds) I'd really like to save myself some clicks here.

    Any ideas?

  • No one? 🙁

  • Just a shot in the dark but try an if exists option

    DECLARE @dbname nvarchar(128)

    SET @dbname = N'Production'

    IF (EXISTS (SELECT name

    FROM master.dbo.sysdatabases

    WHERE ('[' + name + ']' = @dbname

    OR name = @dbname)))

  • I have tried, but the parser doesn't like that either. 🙁

  • We've tried several options, but it really seems as if a dynamic 'use' statement can't be run inside SSMS2012.

    We are considering the use of synonyms and linked servers and are planning to implement this. It will mean having to change all 300 feeds.

    I will post here when we reach a solution.

  • bas de zwart (6/6/2013)


    We've tried several options, but it really seems as if a dynamic 'use' statement can't be run inside SSMS2012.

    We are considering the use of synonyms and linked servers and are planning to implement this. It will mean having to change all 300 feeds.

    I will post here when we reach a solution.

    The problem is that the dynamic SQL statement runs in its own separate execution context - statements executed in that context will be affected by the USE command, but the execution context of the dynamic SQL statement will not affect the execution context of the batch that prepared and submitted the dynamic SQL for execution.

    With one exception, our DEV environments use the exact same schema as our PROD environments (and the QA and UAT environments in between) - same DB and object names for everything. That way, we don't have to change any code when it migrates from environment to environment. In that one environment (for which I am currently developing some processes, unfortunately), an important database is named differently in DEV than in PROD, so all three-part qualified object names and any USE statements involved must be changed when the code migrates - an enormous hassle and potential source of deployment problems.

    If possible, you should change the database names to be consistent from environment to environment - one schema change will be less painful over the long run than making all the changes every time you deploy new code to PROD.

    Jason Wolfkill

Viewing 6 posts - 1 through 5 (of 5 total)

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