Contents
I. Backup.................................................................................................................................................2
1. Backup Database Template.............................................................................................................2
2. Backup Filegroups Template............................................................................................................2
II. Database..............................................................................................................................................2
1. Attach database template...............................................................................................................2
2. Create database template...............................................................................................................3
3. Detach database template...............................................................................................................3
4. Drop Database Template.................................................................................................................3
III. Trigger.............................................................................................................................................4
1. Create database trigger template....................................................................................................4
2. Alter database trigger template......................................................................................................4
I. Backup
-- ===========================
1. Backup Database Template
-- ===========================
BACKUP DATABASE <Database_Name, sysname, Database_Name>
TO DISK = N'<Backup_Path,,C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Backup\><Database_Name, sysname, Database_Name>.bak'
WITH
NOFORMAT,
NOINIT,
NAME = N'<Database_Name, sysname, Database_Name>-Full Database Backup',
SKIP,
STATS = 10;
GO
-- =============================
2. Backup Filegroups Template
-- =============================
USE master
GO
BACKUP DATABASE <Database_Name, sysname, Database_Name>
FILE = N'<Logical_File_Name_1,sysname,Logical_File_Name_1>',
FILEGROUP = N'PRIMARY',
FILE = N'<Logical_File_Name_2, sysname, Logical_File_Name_2>',
FILEGROUP = N'<Filegroup_1, sysname, Filegroup_1>'
TO <Backup_Device_Name, sysname, Backup_Device_Name>
GO
-- ================================
3. Create Backup Device Template
-- ================================
USE master
GO
EXEC master.dbo.sp_addumpdevice
@devtype = N'disk',
@logicalname = N'<Backup_Device_Name, SYSNAME, Backup_Device_Name>',
@physicalname = N'C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Backup\<Backup_Device_Name, SYSNAME,
Backup_Device_Name>.bak'
GO
II. Database
--=====================================
1. Attach database template
--=====================================
IF NOT EXISTS(
SELECT *
FROM sys.databases
WHERE name = N'<database_name, sysname, your_database_name>'
)
CREATE DATABASE <database_name, sysname, your_database_name>
ON PRIMARY (FILENAME = '<database_primary_file_path,,C:\Program
files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\your_database_name.MDF>')
FOR ATTACH
GO
-- =============================================
2. Create database template
-- =============================================
USE master
GO
-- Drop the database if it already exists
IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'<Database_Name, sysname, Database_Name>'
)
DROP DATABASE <Database_Name, sysname, Database_Name>
GO
CREATE DATABASE <Database_Name, sysname, Database_Name>
GO
--=====================================
3. Detach database template
--=====================================
EXEC sp_detach_db @dbname = '<database_name, sysname, your_database_name>'
,@skipchecks = 'true'
,@KeepFulltextIndexFile = 'true'
GO
-- =========================
4. Drop Database Template
-- =========================
USE master
GO
IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'<Database_Name, sysname, Database_Name>'
)
DROP DATABASE <Database_Name, sysname, Database_Name>
GO
III. Trigger
--====================================
1. Create database trigger template
--====================================
USE <database_name, sysname, AdventureWorks>
GO
IF EXISTS(
SELECT *
FROM sys.triggers
WHERE name = N'<trigger_name, sysname, table_alter_drop_safety>'
AND parent_class_desc = N'DATABASE'
)
DROP TRIGGER <trigger_name, sysname, table_alter_drop_safety> ON
DATABASE
GO
CREATE TRIGGER <trigger_name, sysname, table_alter_drop_safety> ON DATABASE
FOR <data_definition_statements, , DROP_TABLE, ALTER_TABLE>
AS
IF IS_MEMBER ('db_owner') = 0
BEGIN
PRINT 'You must ask your DBA to drop or alter tables!'
ROLLBACK TRANSACTION
END
GO
--====================================
2. Alter database trigger template
--====================================
USE <database_name, sysname, AdventureWorks>
GO
ALTER TRIGGER <trigger_name, sysname, table_alter_drop_safety> ON DATABASE
FOR <data_definition_statements, , DROP_TABLE, ALTER_TABLE>
AS
IF IS_MEMBER ('db_owner') = 0
BEGIN
PRINT 'You must ask your DBA to drop or alter tables!'
ROLLBACK TRANSACTION
END
GO
--====================================
3. Drop database trigger template
--====================================
IF EXISTS(
SELECT *
FROM sys.triggers
WHERE name = N'<trigger_name, sysname, your_trigger_name>'
AND parent_class_desc = N'DATABASE'
)
DROP TRIGGER <trigger_name, sysname, your_trigger_name> ON DATABASE
GO
IV. Index
-- =============================================
1. Create index basic template
-- =============================================
USE <database_name, sysname, AdventureWorks>
GO
CREATE INDEX <index_name, sysname, ind_test>
ON <schema_name, sysname, Person>.<table_name, sysname, Address>
(
<column_name1, sysname, PostalCode>
)
GO
V. Restore
-- ============================
1. Restore Database Template
-- ============================
USE master
GO
RESTORE DATABASE <Database_Name, sysname, Database_Name>
FROM DISK = N'<Backup_Path,,C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Backup\><Database_Name, sysname, Database_Name>.bak'
WITH
FILE = 1,
NOUNLOAD,
REPLACE,
STATS = 10
GO
-- =======================================
2. Restore File and Filegroups template
-- =======================================
-- Create database using three file groups, including PRIMARY
CREATE DATABASE [<database_name, sysname, TestDB>] ON
PRIMARY (
Name= N'<database_name, sysname, TestDB>_data',
FILENAME= N'<file_location, sysname, D:\Program Files\Microsoft
SQL Server\MSSQL.1\MSSQL\DATA\><database_name, sysname, TestDB>_data.mdf'),
FILEGROUP [<filegroup_name1, sysname, FG_1>] (
NAME=N' <database_name, sysname, TestDB>_<filegroup_name1,
sysname, FG_1>',
FILENAME =N'<file_location, sysname, D:\Program Files\Microsoft
SQL Server\MSSQL.1\MSSQL\DATA\><database_name, sysname,
TestDB>_<filegroup_name1, sysname, FG_1>.ndf'),
FILEGROUP [<filegroup_name2, sysname, FG_2>] (
NAME=N' <database_name, sysname, TestDB>_<filegroup_name2,
sysname, FG_2>',
FILENAME =N'<file_location, sysname, D:\Program Files\Microsoft
SQL Server\MSSQL.1\MSSQL\DATA\><database_name, sysname,
TestDB>_<filegroup_name2, sysname, FG_2>.ndf')
LOG ON (
NAME =N'<database_name, sysname, TestDB>_log',
FILENAME =N'<file_location, sysname, D:\Program Files\Microsoft
SQL Server\MSSQL.1\MSSQL\DATA\><database_name, sysname, TestDB>_log.ldf');
GO
-- Backup database
BACKUP DATABASE [<database_name, sysname, TestDB>]
FILEGROUP=N'PRIMARY',
FILEGROUP=N'<filegroup_name1, sysname, FG_1>',
FILEGROUP=N'<filegroup_name2, sysname, FG_2>'
TO DISK = N'<file_location, sysname, D:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\><database_name, sysname, TestDB>.bak'
-- Backup log
BACKUP LOG [<database_name, sysname, TestDB>]
TO DISK = '<file_location, sysname, D:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\><database_name, sysname, TestDB>.bak'
WITH
NO_TRUNCATE ,
NORECOVERY,
NOFORMAT,
NOINIT,
NAME = '<database_name, sysname, TestDB> backup',
SKIP,
NOREWIND,
NOUNLOAD,
STATS = 10
-- Restore filegroups - one at a time
RESTORE DATABASE [<database_name, sysname, TestDB>]
FILE = N'<database_name, sysname, TestDB>_data'
FROM DISK = N'<file_location, sysname, D:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\><database_name, sysname, TestDB>.bak'
WITH
FILE = 1,
NORECOVERY,
NOUNLOAD,
STATS = 10
GO
RESTORE DATABASE [<database_name, sysname, TestDB>]
FILE = N' <database_name, sysname, TestDB>_<filegroup_name1, sysname,
FG_1>'
FROM DISK = N'<file_location, sysname, D:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\><database_name, sysname, TestDB>.bak'
WITH
FILE = 1,
NORECOVERY,
NOUNLOAD,
STATS = 10
GO
RESTORE DATABASE [<database_name, sysname, TestDB>]
FILE = N' <database_name, sysname, TestDB>_<filegroup_name2, sysname,
FG_2>'
FROM DISK = N'<file_location, sysname, D:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\><database_name, sysname, TestDB>.bak'
WITH
FILE = 1,
NORECOVERY,
NOUNLOAD,
STATS = 10
-- Restore log
RESTORE LOG [<database_name, sysname, TestDB>]
FROM DISK = N'<file_location, sysname, D:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\><database_name, sysname, TestDB>.bak'
WITH
FILE = 2,
NOUNLOAD,
STATS = 10
GO
-- =============================================
3. Create basic stored procedure template
-- =============================================
-- Drop stored procedure if it already exists
IF EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE SPECIFIC_SCHEMA = N'<Schema_Name, sysname, Schema_Name>'
AND SPECIFIC_NAME = N'<Procedure_Name, sysname, Procedure_Name>'
)
DROP PROCEDURE <Schema_Name, sysname, Schema_Name>.<Procedure_Name,
sysname, Procedure_Name>
GO
CREATE PROCEDURE <Schema_Name, sysname, Schema_Name>.<Procedure_Name, sysname,
Procedure_Name>
<@param1, sysname, @p1> <datatype_for_param1, , int> =
<default_value_for_param1, , 0>,
<@param2, sysname, @p2> <datatype_for_param2, , int> =
<default_value_for_param2, , 0>
AS
SELECT @p1, @p2
GO
-- =============================================
-- Example to execute the stored procedure
-- =============================================
EXECUTE <Schema_Name, sysname, Schema_Name>.<Procedure_Name, sysname,
Procedure_Name> <value_for_param1, , 1>, <value_for_param2, , 2>
GO
-- ==================================
4. Alter Stored Procedure Template
-- ==================================
ALTER PROCEDURE <Schema_Name, sysname, Schema_Name>.<Procedure_Name, sysname,
Procedure_Name>
<@param1, sysname, @p1> <datatype_for_param1, , int> =
<default_value_for_param1, , 0>,
<@param2, sysname, @p2> <datatype_for_param2, , int> =
<default_value_for_param2, , 0>
AS
SELECT @p1, @p2
GO
-- =============================================
-- Example to execute the stored procedure
-- =============================================
EXECUTE <Schema_Name, sysname, Schema_Name>.<Procedure_Name, sysname,
Procedure_Name> <value_for_param1, , 1>, <value_for_param2, , 2>
GO
-- =======================================================
5. Drop Stored Procedure
-- =======================================================
-- Drop stored procedure if it already exists
IF EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE SPECIFIC_SCHEMA = N'<Schema_Name, sysname, Schema_Name>'
AND SPECIFIC_NAME = N'<Procedure_Name, sysname, Procedure_Name>'
)
DROP PROCEDURE <Schema_Name, sysname, Schema_Name>.<Procedure_Name,
sysname, Procedure_Name>
GO