Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamically change database depending on the server?


Dynamically change database depending on the server?

Author
Message
bas de zwart
bas de zwart
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 253
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?
bas de zwart
bas de zwart
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 253
No one? Sad
hvermaak
hvermaak
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
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)))
bas de zwart
bas de zwart
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 253
I have tried, but the parser doesn't like that either. Sad
bas de zwart
bas de zwart
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 253
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.
wolfkillj
wolfkillj
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 Visits: 2582
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search