sql 数据库备份脚本+ StoredProcedure.sql
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 数据库备份脚本+ StoredProcedure.sql相关的知识,希望对你有一定的参考价值。
--#######################################################################################
-- BACKUP DATABASE SCRIPT FOR Local/ Networked / Remote Server (LAN)
--#######################################################################################
-- EX-1:
-- To run this query, you have to login as 'sa' (or you have to have enough permission to create backup)
-- ex: BACKUP DATABASE AdvancedTSQL TO DISK = 'D:\FinCrime.BAK'
DECLARE @DatabaseName NVARCHAR(MAX) = 'UCB';
DECLARE @BackupPath NVARCHAR(MAX) = '\\192.168.1.254\ds-shared-drive-2\AMLShare\';
DECLARE @Query NVARCHAR(MAX) = 'BACKUP DATABASE '+@DatabaseName+' TO DISK = '''+@BackupPath + @DatabaseName+ '.bak' + ''''
--PRINT @Query
EXEC(@query)
-- EX-2 (Stored Procedure):
-- EXEC #BackupDatabase 'UCB', '\\192.168.1.254\ds-shared-drive-2\AMLShare\'
-- EXEC BackupDatabase 'FinCrime', 'D:\'
-- DROP PROCEDURE #BackupDatabase
CREATE PROCEDURE #BackupDatabase
@DatabaseName NVARCHAR(MAX),
@BackupLocation NVARCHAR(MAX) = 'D:\'
AS
--SET @BackupLocation NVARCHAR(MAX) = '\\192.168.1.254\ds-shared-drive-2\AMLShare\'
--SET @BackupLocation NVARCHAR(MAX) = 'D:\db_BAK\'
DECLARE @dateTime NVARCHAR(MAX) = (SELECT CONVERT(VARCHAR(24),GETDATE(),102) + ' ' +REPLACE(FORMAT(CAST(GETDATE() AS DATETIME),'hh:mm:ss tt'),':','.'))
DECLARE @backupFile NVARCHAR(MAX) = @backupLocation + REPLACE(REPLACE(@DatabaseName, '[',''),']','')+ '_FULL_'+ @dateTime + '.bak'
DECLARE @backupName NVARCHAR(MAX) = REPLACE(REPLACE(@DatabaseName,'[',''),']','') +' FULL BACKUP FOR '+ @dateTime
DECLARE @sqlCommand NVARCHAR(MAX) = 'BACKUP DATABASE ' +@DatabaseName+ ' TO DISK = ''' +@BackupFile+ ''' WITH INIT, NAME= ''' +@BackupName+ ''', NOSKIP, NOFORMAT'
PRINT @sqlCommand
EXEC(@sqlCommand)
-- EX-3:
-- BACKUP DATABASE SCRIPT FOR Local/network
-- ex: BACKUP DATABASE AdvancedTSQL TO DISK = 'D:\AdvancedTSQL.BAK'
--DECLARE @BackupPath NVARCHAR(MAX) = '\\192.168.1.254\ds-shared-drive-2\AMLShare\'
--DECLARE @DatabaseName NVARCHAR(MAX) = 'FinCrime'
--DECLARE @FileName NVARCHAR(MAX) = 'FinCrime.bak'
--DECLARE @Query NVARCHAR(MAX) = 'BACKUP DATABASE '+@DatabaseName+' TO DISK = '''+@BackupPath + @FileName+''''
--PRINT @Query
--EXEC(@query)
以上是关于sql 数据库备份脚本+ StoredProcedure.sql的主要内容,如果未能解决你的问题,请参考以下文章
sql 数据库备份脚本+ StoredProcedure.sql
sql 用于备份数据库的SQL脚本
sqlserver脚本备份固定多个数据库
mysql备份sql,脚本
sqlserver2008数据库自动备份的sql脚本及使用bat命令执行脚本
sqlserver2008数据库自动备份的sql脚本及使用bat命令执行脚本