Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 5123

T-SQL USE : Reference Database in Different Server

$
0
0

I'm fairly new to SQL so perhaps I am missing something but what I am trying to do is have a script I wrote select a certain database depending on @@ServerName.  This script will be run by the testing department and needs to work regardless of connection.

IF @@SERVERNAME IN ('X')
    USE DataBaseA
ELSE IF @@SERVERNAME IN ('Y')
    USE DataBaseB
ELSE
    USE DataBaseC

What happens is DataBaseB does not exist on server X so I receive the error of "no database exists".  As well, DataBaseC lacks certain columns in tables that DataBaseA has (DataBaseC will later get these columns but are not there right now).  Is there a different way to force the code to properly use the right database and not throw errors?


Viewing all articles
Browse latest Browse all 5123

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>