views:

429

answers:

1

Dear guys

I want to backup a SQL Server database with C#. I have written a stored procedure which backup the database:

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

    -- Insert statements for procedure here
    BACKUP DATABASE WINC_kentico
    TO DISK = 'G:\MSSQL10.MSSQLSERVER\MSSQL\Backup\WINC_kentico_' + NOW() + '.bak'
    WITH NOINIT
END
GO

In line 10 I set the path for the backup. I want to add the current DateTime at the end of the file name. How is the syntax to set a DataTime at the end of the file name?

Best regards

+4  A: 
DECLARE @NAME VARCHAR(250);
SET @NAME = 'G:\MSSQL10.MSSQLSERVER\MSSQL\Backup\WINC_kentico_' + CONVERT(VARCHAR,GETDATE(),112) + '.bak';
BACKUP DATABASE WINC_kentico
TO DISK = @NAME
WITH NOINIT
LukLed
Good call on using a cast format that does not create invalid file names (ie. ':' free).
Remus Rusanu