Command to create a new Db as a backup/clone of existing DB:
Connect to the master DB and execute the following command:
CREATE DATABASE <newDBname> AS COPY OF <name of DB to be backed up>;
Eg: CREATE DATABASE DB2 AS COPY OF DB1
One important thing to note is that the actual Db copy wouldn't be completed even if the command complete successfully. Inorder to check the status of the copying , you can use the following command
SELECT name, state, state_desc FROM sys.databases WHERE name = 'Databasenew'
The value of State_desc column in the output will be 'online' when the copying is completed and DB is ready for use.The status will be shown as 'copying' when the DB copy is in progress
Rename database:
Again you need to connect to the masterDB and execute the following query
USE master;
GO
ALTER DATABASE <DB name>
Modify Name = <new DB name> ;
GO
Rename Table:
If you need to rename a table in a DB, use the following command after connecting to the DB
sp_rename '<tablename>', '<tablename-new>'
Connect to the master DB and execute the following command:
CREATE DATABASE <newDBname> AS COPY OF <name of DB to be backed up>;
Eg: CREATE DATABASE DB2 AS COPY OF DB1
One important thing to note is that the actual Db copy wouldn't be completed even if the command complete successfully. Inorder to check the status of the copying , you can use the following command
SELECT name, state, state_desc FROM sys.databases WHERE name = 'Databasenew'
The value of State_desc column in the output will be 'online' when the copying is completed and DB is ready for use.The status will be shown as 'copying' when the DB copy is in progress
Rename database:
Again you need to connect to the masterDB and execute the following query
USE master;
GO
ALTER DATABASE <DB name>
Modify Name = <new DB name> ;
GO
Rename Table:
If you need to rename a table in a DB, use the following command after connecting to the DB
sp_rename '<tablename>', '<tablename-new>'
Comments
Post a Comment