I use a simple script to backup database in SQL Server:
BACKUP DATABASE AdventureWorks
TO DISK = 'C:\AdventureWorks.BAK'
GO
My goal is to backup several databases using a script.
This will be used to different clients so I have use parameters that I can edit every time I need to run the script.
SQL Server says that my script has a syntax error.
Can you guys check my query, please?
declare @loc nvarchar(200) = 'D:\BAK\'
declare @client nvarchar(50) = 'CLIENT001_'
declare @date nvarchar(50) = '2020-02-29'
BACKUP DATABASE DB1
TO DISK = @loc + @client + 'DB1_' + @date + '.BAK'
GO
BACKUP DATABASE DB2
TO DISK = @loc + @client + 'DB2_' + @date + '.BAK'
GO
BACKUP DATABASE DB3
TO DISK = @loc + @client + 'DB3' + @date + '.BAK'
GO
BACKUP DATABASE DB4
TO DISK = @loc + @client + 'DB4' + @date + '.BAK'
GO
EXEC sp_executesqlto execute it