Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Database Backup through Batch File

Author  Topic 

sunny_10
Yak Posting Veteran

72 Posts

Posted - 2014-03-07 : 01:29:10
hi

Below is the stored procedure & then is the Batch file . Batch file is executing but it is not creating any backup

Create PROCEDURE [dbo].[Sp_Backup]
-- Add the parameters for the stored procedure here
@path VARCHAR(256) ,
@dbname varchar(50)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- specify database backup directory
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name

--SET @path = 'C:\Backup\'
-- specify filename format
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)

DECLARE db_cursor CURSOR FOR
SELECT name FROM master.dbo.sysdatabases
WHERE name IN (@dbname)

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname

WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @dbname + '_' + @fileDate + '.BAK'
BACKUP DATABASE @dbname TO DISK = @fileName

FETCH NEXT FROM db_cursor INTO @dbname
END
CLOSE db_cursor
DEALLOCATE db_cursor
END


sqlcmd -S ABC -Q "EXEC sp_Backup @dbname='TEST', @path='D:\Backup\'"

Thanks

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2014-03-07 : 09:46:41
The WHERE name IN (@dbname) will not work as you think.

If only one name is being used most of your logic can go away.

Create a dynamic SQL statement for the BACKUP and execute that.

djj
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2014-03-08 : 03:12:12
I assume the purpose of the procedure is to complete one database backup. In that case , why are you using the the CURSOR function. You can delete all that logic.
If , on the other hand , you need to backup all databases - use : http://www.sqlserver-dba.com/2008/08/backup-all-data.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page
   

- Advertisement -