1

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

1 Answer 1

7
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
Sign up to request clarification or add additional context in comments.

1 Comment

Good call on using a cast format that does not create invalid file names (ie. ':' free).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.