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 .....
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.
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.
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.
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.
Put a GO statement after the CREATE...
...
CREATE DATABASE Arms2;
PRINT 'CREATE DATABASE Arms2'
GO
USE Arms2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With