I have made below stored procedure for taking the backup of a database
CREATE PROCEDURE [dbo].[usp_databasebackup]
@DatabaseName NVARCHAR(200),
@Path NVARCHAR(500)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sqlQuery VARCHAR(5000)
BEGIN
SET @sqlQuery = ' BACKUP DATABASE ['+@DatabaseName+'] TO DISK = N''D:\@DatabaseName.bak''
WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''@DatabaseName-Full Database Backup'',
SKIP, NOREWIND, NOUNLOAD, STATS =''10'' '
EXEC (@sqlQuery)
END
END
GO
When I execute the stored procedure with a database name and path as parameters, I get this error:
Operand type clash: varchar is incompatible with int
'...[' + @SomeString + ']...'is not injection safe! Whenever you inject dynamic objects always properly quote them withQUOTENAME.N''D:\@DatabaseName.bak''is also not going to work; SQL isn't a script language, so it won't replace the value of@DatabaseName(which is undefined) with the value of@DatabaseName. Have a look here at how to inject values into a dynamic statement. - LarnuSTATS =expect an integer instead of a string? What happens when you manually "fill in the blanks" in your backup statement and try to execute it? - HeinziStatsshould not be quoted. You don't even use@path. You don't even need DSQL for this,backup databaseallows you to pass parameters for database and path. - Stu@DatabaseNameshould be defined as asysname, a synonym fornvarchar(128) NOT NULL, which is the data type SQL Server uses for all object names. - Larnu