how to switch database through script (Use database doesn't support) RRS feed

  • Question

  • Hi,

    I have  SQL azure  database script and now i want to run it into  SQL azure account using (SQL Server R2) tool,

    Now i am not able to identify how to select that particular database since "Use databasename" does not support ...

    Please let me know if any other alternate ...

    Like below :

    Use MyDB

    Create table tb1(Id int);

    but its giving error saying Use statement does not support ...

    so how i can switch database through script..






    Thursday, November 18, 2010 5:08 AM


All replies

  • Hi,

    You are right, USE <Database> isn't supported. Why do you need to switch database through script? If you are using SQL Server R2 you can manually switch databases by using the selection box in the upper left corner. If that isn't working for your scenario and you really need scripting you might want to use the "sqlcmd" option. In that case you can provide the correct database as a paramter and make sure you connect to the database you want. Once you have the connection you can run your scripts against that database.

    Is this what you are looking for or I am completely missing your point? :)

    Hope this helps.


    • Proposed as answer by SarangKulkarni Thursday, November 18, 2010 6:18 AM
    Thursday, November 18, 2010 6:15 AM
  • Yup I guess Edward sums it up perfectly. The SQLCMD mode can be activated from the "Query" menu in SSMS. Here's a SQLCMD primer http://msdn.microsoft.com/en-us/library/ms162773.aspx 
    Thursday, November 18, 2010 6:18 AM
  • Hi Edward,

    Thanks a lot for valuable reply and you got me correctly what i am expecting.

    I also responded same thing to  my client who runs DB script in azure, but senerio is like if database is not exists then it should create new database at run time and also run the script in same database...for same i have put checkpoint to check database existence and creating through script only  but my  concern is how i switch to newly database and run the remaining script it get executed in default selected database.

    You have  already mentioned some solution which i am trying to implement..


    Hope you understood my requirment..




    Thursday, November 18, 2010 11:45 AM
  • Hey Sarang ...great to see you after a long time..

    Yes i also agree with Edward and trying to implement the same...


    Thursday, November 18, 2010 11:46 AM
  • I used same approach and got solution


    Below links coved implementation code...





    Friday, November 19, 2010 4:07 AM