it is working......
declare @dbName varchar(50)
set @dbName='myDb'
BACKUP DATABASE @dbName TO DISK = 'c:\backup\myDb.bak'
But Why is it not working? where as both are similar query.
BACKUP DATABASE 'mydb' TO DISK = 'c:\backup\myDb.bak'
The documentation says:
BACKUP DATABASE { database_name | @database_name_var }
TO <backup_device> [ ,...n ]
So it takes a database name (f.e. mydb) or a variable (f.e. @dbname.) But not a string literal (f.e. 'mydb'.)
Just omit the quotes to change the string literal to a database name:
BACKUP DATABASE mydb TO DISK = 'c:\backup\myDb.bak'