SQL SERVER2012中使用遊標來備份資料庫

陸飛(Fei.Lu)發表於2014-07-12

在SQL SERVER中,新增JOB,可以使用以下語句設定定期備份資料庫。

DECLARE @name VARCHAR(50)--databasename

DECLARE @path VARCHAR(256)--pathforbackupfiles

DECLARE @fileName VARCHAR(256)--filenameforbackup

DECLARE @fileDate VARCHAR(20)--usedforfilename SET @path='E:\Backup' SELECT @fileDate=CONVERT(VARCHAR(20),GETDATE(),112)

DECLARE db_cursor CURSOR FOR  SELECT [name]  FROM master.dbo.sysdatabases  WHERE [name] NOT IN('master','model','msdb','tempdb')  

OPEN db_cursor  FETCH NEXT FROM db_cursor INTO @name  WHILE @@FETCH_STATUS=0  

BEGIN   

SET @fileName=@path+@name+'_'+@fileDate+'.BAK'  

BACKUP DATABASE @name TO DISK=@fileName  

FETCH NEXT FROM db_cursor INTO @name  

END  CLOSE db_cursor  

DEALLOCATE db_cursor

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

DECLARE @name VARCHAR(50)--databasename

DECLARE @path VARCHAR(256)--pathforbackupfiles

DECLARE @fileName VARCHAR(256)--filenameforbackup

DECLARE @fileDate VARCHAR(20)--usedforfilename SET @name='dbname' SET @path='E:\' SELECT @fileDate=CONVERT(VARCHAR(20),GETDATE(),112) SET @fileName=@path+@name+'_'+@fileDate+'.BAK' BACKUP DATABASE @name TO DISK=@fileName ------------------------

相關文章