SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to build a dynamic sproc for database backup?

Execute the following Microsoft SQL Server 2008 T-SQL database scripts in Management Studio Query Editor to demonstrate the architecture and use of dynamic stored procedure for database backup.

-- SQL Server T-SQL full database backup basic syntax
BACKUP DATABASE [AdventureWorks2008] TO  DISK = N'D:\backup\AdventureWorks2008\aw8.bak'
GO
-- SQL Server T-SQL full database backup syntax with options
BACKUP DATABASE [AdventureWorks2008] TO  DISK = N'D:\backup\AdventureWorks2008\aw8.bak'
WITH NOFORMAT, NOINIT,  NAME = N'AdventureWorks2008-Full Database Backup',
SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO
------------
-- SQL Server T-SQL transaction log backup basic syntax
BACKUP LOG [AdventureWorks2008] TO  DISK = N'D:\backup\AdventureWorks2008\aw8_20120201_1430.trn'
GO
------------
-- SQL Server T-SQL differential backup basic syntax
BACKUP DATABASE [AdventureWorks2008] TO  DISK = N'D:\backup\AdventureWorks2008\aw8.dif'
WITH  DIFFERENTIAL
GO
------------

 

-- T-SQL dynamic stored procedure for database backup

USE AdventureWorks2008;

GO

 

CREATE PROC sprocFullBackup

           @DatabaseName SYSNAME,

           @Folder       VARCHAR(256),

           @FileName     SYSNAME

AS

  BEGIN

    DECLARE  @DynaSql NVARCHAR(MAX)

    IF RIGHT(@Folder,1) != '\'

      SET @Folder = @Folder + '\'

    SELECT @DynaSql = 'BACKUP DATABASE ' + @DatabaseName + ' TO DISK = ' +

                   CHAR(39) + @Folder + @FileName + CHAR(39)

    PRINT @DynaSql

    EXEC sp_executeSQL @DynaSql

  END

GO

 

-- Execute dynamic stored procedure - Transact-SQL

exec sprocFullBackup 'AdventureWorks2008',

                     'f:\data\AdventureWorks2008\backup\',

                     'test1.bak'

 

/*

BACKUP DATABASE AdventureWorks2008 TO DISK

        = 'f:\data\AdventureWorks2008\backup\test1.bak'

 

Processed 23288 pages for database 'AdventureWorks2008',

file 'AdventureWorks2008_Data' on file 1.

Processed 36 pages for database 'AdventureWorks2008',

file 'FileStreamDocuments' on file 1.

Processed 1 pages for database 'AdventureWorks2008',

file 'AdventureWorks2008_Log' on file 1.

BACKUP DATABASE successfully processed 23325 pages

in 4.664 seconds (39.069 MB/sec).

*/

------------

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.