Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Dynamically change database depending on the server? Expand / Collapse
Author
Message
Posted Monday, June 3, 2013 8:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 19, 2013 7:44 AM
Points: 38, Visits: 162
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?
Post #1459315
Posted Wednesday, June 5, 2013 5:02 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 19, 2013 7:44 AM
Points: 38, Visits: 162
No one?
Post #1460158
Posted Wednesday, June 5, 2013 5:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, October 16, 2013 6:41 AM
Points: 25, Visits: 167
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)))

Post #1460164
Posted Thursday, June 6, 2013 1:48 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 19, 2013 7:44 AM
Points: 38, Visits: 162
I have tried, but the parser doesn't like that either.
Post #1460559
Posted Thursday, June 6, 2013 3:19 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 19, 2013 7:44 AM
Points: 38, Visits: 162
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.
Post #1460594
Posted Thursday, June 6, 2013 10:07 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, October 17, 2014 10:49 AM
Points: 1,061, Visits: 2,569
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
Blog: SQLSouth
Twitter: @SQLSouth
Post #1460786
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse