Backup toàn bộ database trong SQL Server

Theo: nguyenhaidang.name.vn | 01/09/2022 - 10:33

 Lệnh backup toàn bộ database trong SQL Server

 

DECLARE @bk_path VARCHAR(256);

DECLARE @db_name VARCHAR(50);

DECLARE @bk_file_date VARCHAR(20);

DECLARE @bk_file_name VARCHAR(256);

DECLARE @i INT = 1;

 

SET @bk_path = ''C:\WORKSPACE\DATABASE\BACKUP\'';

SELECT @bk_file_date = FORMAT(GETDATE(), ''yyyyMMdd'');

 

DECLARE @db_names TABLE (

   id INT IDENTITY(1,1) PRIMARY KEY, 

   db_name VARCHAR(50) NOT NULL );

 

INSERT INTO @db_names

   SELECT name 

   FROM master.sys.databases 

   WHERE name NOT IN (''master'',''model'',''msdb'',''tempdb'')

      AND state = 0

      AND is_in_standby = 0; 

 

WHILE EXISTS (SELECT 1 from @db_names WHERE Id = @i)

BEGIN

    

   SELECT @db_name = db_name from @db_names WHERE Id = @i;  

   SET @bk_file_name = @bk_path + @db_name + ''_'' + @bk_file_date + ''.BAK''; 

   BACKUP DATABASE @db_name TO DISK = @bk_file_name WITH STATS; 

   SET @i = @i + 1;

    

END

GO

 
Back Head Print
Tin khác

Search GridView with Paging on TextBox KeyPress using jQuery in ASP.Net    (28/07/2010)

Bootstrap AutoComplete TextBox example using jQuery TypeAhead plugin in ASP.Net with C# and VB.Net    (28/07/2010)

Disable Button and Submit button after one click using JavaScript and jQuery    (29/07/2010)

Split and convert Comma Separated (Delimited) String to Table in SQL Server    (01/09/2010)

Select Column values as Comma Separated (Delimited) string in SQL Server using COALESCE    (01/09/2010)