How to database back with sql qury
Microsoft Net Framework
  • .Net
  • 421
  • February-11-2018
  • by 

How to database back with sql qury

 

USE [DBBackupDatabase]
GO
/****** Object:  StoredProcedure [dbo].[TakeDatabaseBAckup]    Script Date: 2/11/2018 9:12:25 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

 

DECLARE @name VARCHAR(50) -- database name  
DECLARE @path VARCHAR(256) -- path for backup files  
DECLARE @fileName VARCHAR(256) -- filename for backup  
DECLARE @fileDate VARCHAR(20) -- used for file name

 
-- specify database backup directory
SET @path = 'C:\BackupDBs\'  
 
-- specify filename format
SELECT @fileDate = FORMAT (GETDATE(),'MM_dd_yyyy_hh_mm')

 
DECLARE db_cursor CURSOR READ_ONLY FOR  
SELECT name 
FROM master.dbo.sysdatabases 
--WHERE name   IN ('softwarecmdb011','SSS006','starsacademydb010','starscollegedb010','starsschooldb010','unitedacademydb010')   

 include WHERE name   like '%_01%'
 
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

 

 

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM