Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How - create and use database directly after creation in SQL Server?

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 .....
like image 223
Mehdi Bugnard Avatar asked Jan 24 '13 10:01

Mehdi Bugnard


People also ask

How to create a database in SQL Server?

To create a database. In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that instance. Right-click Databases, and then click New Database. In New Database, enter a database name. To create the database by accepting all default values, click OK; otherwise, continue with the following optional steps.

What is the purpose of the create database statement?

In SQL, the CREATE DATABASE statement is used to create a database though the ANSI standard does not contain a CREATE DATABASE statement. The database names are case sensitive in Unix but this restriction does not apply in Windows. This is also true for table names.

How to create user user database in SQL Server?

User databases are created by users (DBAs, and testers who have access to create database). To create a database, the below methods could be used – SQL Server Management Studio. Transact-SQL. Connect to an SQL instance of the SQL Server Database Engine then expand that instance. Right-click Databases, and then click New Database.

How to create a database in SQL Server 2019 using SQLCMD?

Now lets see the steps for creating a Database in SQL Server 2019 using sqlcmd Step 1. Run the Windows Command Prompt and execute the following command to connect to a SQL Server Database instance. The number 1> means that it is connected and ready to receive sentences to execute Step 2.


1 Answers

Put a GO statement after the CREATE...

...
CREATE DATABASE Arms2;
PRINT 'CREATE DATABASE Arms2'
GO
USE Arms2
like image 182
AdaTheDev Avatar answered Sep 19 '22 12:09

AdaTheDev