2

I created a schema script for an sql server database called test.

I want to execute this script in the same server where the test database is found, but for sure with different name, suppose test2.

when I opened the scripts, it starts by CREATE DATABASE [test] and the name test is used many times in the script. so how to safely change database name in the script without affecting the original database?

Note: changing name by just replacing it's name is not a solution because it's name is a part of many procedures and functions

1 Answer 1

3

No need to use database name in each and every query. Just use

USE [Database_Name]

in the above of the script file, then it will consider the database for the entire script until you specify another database name inside the script.

Eg:-

USE My_Database_1

Script_1
Script_2
.
.
Script_n
--Another Database if required
USE My_Database_2

Script_1
Script_2
.
.
Script_n
Sign up to request clarification or add additional context in comments.

2 Comments

and I'll leave CREATE DATABASE [test] , ALTER DATABASE [test] , etc in the same original name without any worry about affecting the original database data?
No, for CREATE/ALTER DATABASE, you have to specify the database. After that you can use the key USE [Database_Name] for your schema (table, procedure, views, udf etc).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.