Changing Databases in SSMS

  • We all know the command to change from one table to another in SQL code:

    USE Table1

    GO

    USE Table2

    GO

    My question is this: Is there a way to change from one database to another using code like this?

    Thanks.

    Steve

  • sdownen05 (6/6/2011)


    We all know the command to change from one table to another in SQL code:

    USE Table1

    GO

    USE Table2

    GO

    My question is this: Is there a way to change from one database to another using code like this?

    Thanks.

    Steve

    you cannot "use" a table, only a database is valid after the USE command; the commands you posted onyl work if you have two databases named 'Table1] and [Table2]

    you can use a semicolon instead of the GO if you want:

    USE SandBox; SELECT * FROM sys.tables ; --returns tables int he sandbox database

    USE master; SELECT * FROM sys.tables --returns tables in the master database

    [/code]

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • It is definitely Monday morning.

    Thanks for the correction. You are right. What I want to do is change to a different instance. Does this make sense?

  • Disconnect and reconnect. Or look at SQLCMD mode. There's no T-SQL command that will let you switch instances

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • What you need to do in new query window is right click > Connection > Change Connection, this will change the instance you are connected to.

    __________________________________________________________________________________
    Steve J
    Jnr Developer
    BSc(Hon)

  • I'm trying to automate a process. That wouldn't work.

  • GilaMonster (6/6/2011)


    Or look at SQLCMD mode. There's no T-SQL command that will let you switch instances

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • So sorry Gail. I forgot to thank you for your answer. I have already created a program that calls SQLCMD to do what I need to do. You have been a great help.

    Steve

Viewing 8 posts - 1 through 7 (of 7 total)

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