5

I created a sql script to check if a database already exist, if it already exists it deletes and re-creates. After I would like to connect it directly after its creation for creating tables ..

Here is my code but it does not work. He announces an error message

Msg 911, Level 16, State 1, Line 10 Database 'Arms2' does not exist. Make sure that the name is entered correctly.

My script

IF EXISTS (select * from sys.databases where name = 'Arms2')
BEGIN 
    DROP DATABASE Arms2
    PRINT 'DROP DATABASE Arms2'
END
    CREATE DATABASE Arms2;
    PRINT 'CREATE DATABASE Arms2'

USE Arms2

CREATE TABLE .....
Mehdi Bugnard
  • 3,889
  • 4
  • 45
  • 86

1 Answers1

11

Put a GO statement after the CREATE...

...
CREATE DATABASE Arms2;
PRINT 'CREATE DATABASE Arms2'
GO
USE Arms2
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
  • 1
    Thank you very much! Everything works perfectly. Can explain why it was necessary to add "GO"? – Mehdi Bugnard Jan 24 '13 at 10:48
  • 1
    @MehdiBugnard It's a "splitter" for certain Microsoft products (SSMS/OSQL) that tells the system to split the script into two batches. It's not a real keyword, in fact you can change it to something else from the options menu in SSMS. – Bridge Jan 24 '13 at 10:49
  • Also, just to provide the MSDN reference - http://msdn.microsoft.com/en-gb/library/ms188037.aspx – AdaTheDev Jan 24 '13 at 10:51
  • 1
    `GO` is specific to SQL Server Management Studio and so isn't recognised by SQL Server. – Simon Morgan Dec 03 '19 at 10:50