T-SQL USE : Reference Database in Different Server

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?

Interesting. I was not aware of that.
I suggest you resort to dynamic SQL if possible.
Build your SQL command into a variable, write only one USE STATEMENT into it and run it via EXECUTE.
More about dynamic SQL at
MSDN or from Erland Sommerskog.
Another option could be using SSIS and work with variables there.
Or go to powershell and cmd get the Servername there and fire up sqlcmd with the right commands (you can use parameter -d instead of use).
In the end there could be quite a bunch of work involved depending how much you have to do in your script.
Cheers
Martin

Similar Messages

  • Schema compare between 2 databases from different server

    Let me know the method for comparing schema between 2 databases from different server of SQL 2012 and sending that in mail.

    Check redgate:
    Compare SQL Server schemas and deploy differences
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Database Design
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

  • Unix Perl Script To verify a Up and Running Database on Different Server

    Unix Perl Script To verify a Up and Running Database on Different Server
    Hi
    can any one please tell me a solution to verify a Up and Running Database on Different Server other than the one where we run the unix perl script? The perl script should check if the database is running else it must exit.
    Thanks much
    Kiran

    The other best solution would be Enterprise Manager, load the EM on the other machine and install oracle intelligent agent on all the boxes where oracle is running and problem solved.
    FTP is only a File Transfer Protocol, you can upload/download the files but cant execute them.
    Apart from EM the best way is load oracle client and make connection to all the databases.
    AND There are some free oracle monitoring software available I dont know much about them but one is NAGIO (if I am not wrong), try that if you want.
    BTW whats the problem in monitoring the boxes from the same physical box, means just schedule a script using cron on the same physical box where oracle is to either make connection using SQLPLUS or check the processes using "ps" command and if there is anything wrong then send alert from that box only. In this way there is no need to maintain a central monitoring server.
    Daljit Singh

  • Run SSIS Package (SQL Database on Different Server) from Data Manager.

    Hi- How to run a SSIS package from BPC Data Manager -This package connects to another SQL Server Database and creates a text file. This text file is the source to BPC custom tasks CONERTTASK and DUMPLOAD task to load to BPC.
    Here is the  flow of the complete package- Dataflow (Create the text file from a SQL Database)
                                                               CONVERT TASK (Convert the file to BPC Format)
                                                                DUMPLOAD TASK (Load the converted file to BPC)
    Any pointer will be a great help.

    Hello Pam,
    When you run SSIS package with BPC DM it runs on the application server. You don't really have to run a package on a different server in order to get data from a remote database and dump it to a file. That task can be done in your SSIS package using various data sources/destinations. If that's what you are trying to do. The only thing is, your BPC admin user (the one you used to install BPC) has to have an appropriate privileges on a remote server.
    Hope that helps.
    Regards,
    Akim

  • Can we rollback catcpu.sql using flashback database?

    Hello all,
    Can we use FLASHBACK DATABASE to roll back the changes made by the catcpu.sql/catpatch.sql used for upgradation/patching? [ I am aware that oracle provides rollback sql's but just curious. ]
    TIA,
    JJ

    Keep in mind, catpatch is only the second part of the patching process, before that you already patched the software. If you want to have a complete rollback, follow the instructions in the readme file.
    Werner

  • Essbase and database in different server

    Hi,
    It is possible to put the ESSBASE in one server and database(oracle , SQL Server) in another server? It is possible, what are the things i want to do?
    Thanks,
    PC

    Hi,
    Assuming that you've got powerful boxes for this purpose, if you are going to use database server just as a repository database server for say, Planning, then that's fine to have essbase and rdbms on the same server. However, it's not advisable to have essbase on a server where a heavy datawarehouse is co-hosted.
    Alp

  • Restoring a Hot backup using RMAN on a different server.

    Hi
    we are using oracle 10.2.0.4.0 on solaris 10 and using a separate catalog for RMAN backups.
    we have taken a production db Hot backup as follows
    level 0 backup on 28 th dec.
    level 1 on 29,
    level 1 on 30 and
    level 1 on 31 st.
    now today in the month of feb .
    we would like to create a dev. DB on a diffent server which has diffent directory structure, i have to create a db using the above backups to setup the db like on 31 st Jan.
    could you please provide the steps and advice how do i proceed.
    Thansk fot the help.

    Make level 0 and level 1 backups + control files backups + archived redo logs backup available on new host and use RMAN DUPLICATE.
    See examples in http://download.oracle.com/docs/cd/B19306_01/backup.102/b14191/rcmdupdb.htm#i1008564
    Edited by: P. Forstmann on 2 févr. 2010 22:39
    Edited by: P. Forstmann on 3 févr. 2010 07:57

  • Getting portal server name using webdynpro deployed in different server.

    Hi All,
      I have a question on accessing the servername where my portal is running which has an iview created for a webdynpro which is deployed in other server.I have J2ee server1 on which portal is installed and running, now i have developed webdynpro with authentication and deployed to another J2ee server2. In server1 i want to create the iview through which i can access the webdynpro deployed in server2, now in my webdynpro i used NavigationTarget with LinkToURL element where user can navigate to the one of portal page which requires the servername of portal, so how can i get the servername where portal is running(server1) if my webdynpro is deployed in server2.please provide what are the steps rerquired to get this scenario running and also for i used the target propery of linktourl to _self  and _top but its opening in new browser, i want link to open  in same window.Please give your valuable suggestions.
    Thanks,
    Pkv.

    Hi..
    try {
         InetAddress ina = InetAddress.getLocalHost();
          wdComponentAPI.getMessageManager().reportSuccess("Host name is "ina.getHostName()"..Host address "ina.getHostAddress()"...CanonicalHostName   "ina.getCanonicalHostName()"...by name    "+ina.getByName(ina.getHostName()));
         } catch (UnknownHostException e) {
         // TODO Auto-generated catch block
         wdComponentAPI.getMessageManager().reportSuccess(""+e);
    this is the small peice of to know  and tell me ur requirment is on Development Server or Production,for development above code is enough if not than need to do more
    Regards
    Rajesh

  • Idoc between different server

    Hi,
    I have to carry out the outbound process using idoc with 2 different server with same client number.
    How i can do the inbound process in receiver side as well as how  i can assign the logical system into the client number in
    SALE.
    pls answer for this...
    by
    balachander
    Edited by: balachandhru on Jun 29, 2010 11:58 AM

    >
    balachandhru wrote:
    > I have to carry out the outbound process using idoc with 2 different server with same client number.
    You want to post the idocs in the same client !! Why ?

  • How to store jpeg images in SQL server using NI database Connectivity Toolset. Can anyone please help me in this regard.

    Please tell how to store jpeg images in SQL Server using NI Database Connectivity Toolset.

    http://www.w3schools.com/sql/sql_datatypes.asp
    You setup a field as BLOB and store the binary of the picture there. Depending on the database it can be called differently as you can see in the link, in SQL server there's even a Image datatype.
    /Y
    LabVIEW 8.2 - 2014
    "Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
    G# - Free award winning reference based OOP for LV

  • Reference to database and/or server name in '' is not supported in this version of SQL Server

    Hello all
    I am using C# and ADO.NET (native) - Visual Studio 2013.
    I am always using full qualified queries like "SELECT * FROM [database].[schema].[table]"
    Obviously there is a problem when doing this against SQL Azure,
    I get the following error: "Reference to database and/or server name in '' is not supported in this version of SQL Server"
    Interesting fact: With SQL Management Studio, the same query works.
    I need to know if this error is really a true one or not.
    If yes, I can quit using SQL Azure. I would need a SQL Server and not a partial SQL Server :-)
    Thanks and best regards
    Frank

    Hi,
    Here are my suggestions:
    1. Use partially qualified queries, because:
    a. from a security standpoint, you'd want to limit your login's access across the server instance
    b. in order to achieve a even better, you should specify your database as the Initial Catalog directly in the SQL Connection.
    If you take into account these practices, SQL Database doesn't come with any limitation from that point of view.
    However, in regard to 'With SQL Management Studio, the same query works', this is actually only partially true because if you run that query agains ADO.NET, you'll actually end up in switching databases along the query which is obviously not permitted in
    SQL Database since you're running in a hosted black-box version of SQL you're not managing. It's just like running the USE <dbname> command which obviously doesn't work.
    Alex

  • What are the advantage of using Oracle Database when compare to SQL SERVER

    Hi all
    Please tell anyone about
    what are the advantage of using Oracle Database when compare to SQL SERVER
    Thanks in advance
    Balamurugan S

    user12842738 wrote:
    Hi,
    There are various differences between the two.
    1. SQL Server is only Windows, but Oracle runs on almost all Platforms.
    2. You can have multiple databases in SQL Server, but Oracle provides you only one database per instance.Given that the very term 'database' has s different meaning in the two products, this "difference" is absolutely meaningless.
    3. SQL Server provides T-SQL for writing programs, whereas Oracle provides PL/SQLWhich means what? Both products have a procedural programming language. They named them differently, and the languages are not interchangeable. Means nothing in comparing the features/strengths/weaknesses/suitability to purpose.
    4. Backup types in both are the same. (Except Oracle provides an additional backup called Logical Backup.)You make that sound like "Logical Backup" is something more than it is. It is nothing more than an export of the data and metadata. Many experts don't even consider it a backup. I'm sure SQL Server provides the same functionality though they probably call it by some other name.
    5. Both provide High Availability.Well, I guess they both have a suite of features they refer to as "High Availability". But what does that really mean? The devil is in the details. Remember, the two products don't even agree on what constitutes a "database".
    6. Both come in various distributions.???
    >
    If you are going for an Implementation, you can try SQL Server Express Edition and Oracle XE which are free to use.
    Then you can choose whichever is comfortable for your needs.
    Thanks.

  • How to pass database logon info to a Crystal Report in a subreport with different server name using VS C #

    Post Author: fabu1971
    CA Forum: .NET
    I could pass the database logon in Reports with subreports but the reports with subreports with different  server name I can not pass the logon information . Do you have any idea how I can do that to pass the database logon with different database or server name ?

    Post Author: quafto
    CA Forum: .NET
    You can use the Subreports collection of the ReportDocument object to access all the subreports in your main report. These are returned as ReportDocument objects. Once you have your subreport as a ReportDocument you can loop through your Tables collection and set the ConnectionProperties to your appropriate Server/Database. For example here is some pseudo code: ConnectionInfo boConnectionInfo = new ConnectionInfo();boConnectionInfo.ServerName = "serverName";boConnectionInfo.DatabaseName = "databaseName";boConnectionInfo.UserID = "username";boConnectionInfo.Password = "yourpassword"; foreach(ReportDocument boSubreport in mainReport.Subreports){    foreach(Table boTable in boSubreport.Database.Tables)    {        TableLogOnInfo boTableLogOnInfo = boTable.LogOnInfo;          boTableLogOnInfo.ConnectionInfo = boConnectionInfo;          boTable.ApplyLogOnInfo(boTableLogOnInfo);          boTable.Location = "newtablelocation";     }}

  • I want to use the SQL Toolkit of NI and SQL Server as my databasis on a server. Do I need to install a client in each computer I want to handle the data into SQL tables or I need only a ODBC driver?

    I want to use the SQL Toolkit of NI and SQL Server as my databasis on a server. Do I need to install a client in each computer I want to handle the data into SQL tables or I need only a ODBC driver?

    You only need the ODBC driver on each computer. If you are distributing the SQL Toolkit app as an executable and do not install the whole toolkit on each computer, you'll need the SQL Toolkit support files. This is about a dozen files. You can get the list at http://digital.ni.com/public.nsf/websearch/b814be005f9da9258625658700550c75?OpenDocument.

  • Can I use Other database as the repository, Sybase/SQL Server? Urgent!!!

    Hi all,
    Can I use Other database as the repository, Sybase/SQL Server? Urgent!!!
    And Can I use other database store business data and sync with lite?
    Thanks ahead!!!

    Jonathan,
    No, it is not possible to use any other database than Oracle (8.1.7) or later .
    Oracle Lite will only work with Oracle.
    Regards

Maybe you are looking for

  • Open Letter to Palm Regarding the Palm Treo 755p

    Dear Palm, As a devoted Palm user for the past 15+ years, I have been praising the versatility, convenience and user friendly Palm hardware/software to my colleagues and family for as many years. As a result many have purchased Palm products and have

  • Simple question.

    I have a button labeled "call report" on my canvas. I am using forms 6i. How can I call a report? It does not have to have any parameters passed. The report already created is basicaly the result of a select query. Thanks!

  • ISCSI traffic running across Cisco 3750 Switches

    My customer has a small shop with 2 servers runnig iSCSI to a SAN device. They are looking for a switch recommendation and I would like to use a pair of Cisco 3750's, to take advantage of the VSS technology for redundancy,L3 and some other Core requi

  • Datatarget missing in InfoPackage

    Friends, I am on BW 3.1. I am copying data from a cube1 to Cube2. I have created update rule on Cube2 from Cube1 and Update rule are active. When I go into Infosource (of Cube1) to create InfoPackage, i do not see Cube2 in Data Target tab. Am I missi

  • Message received after deadline in BPE

    I have built an integration process containing sending of purchase order and receipt of purchase order response. I included a deadline that will issue an alert if the response is not received in a given time. The process works well if the response is