简体   繁体   中英

SQL Server error "Operand type clash: varchar is incompatible with int "

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

You can backup a database without using Dynamic SQL.

You just need to use the backup command like this

backup database @DatabaseName
to disk=@DiskName
with name=@Name,
compression, format, init, skip, norewind, nounload, stats=10

@DatabaseName should be sysname

You need to prepare / concatenate names and paths prior to using variables in the backup command - ie, @DiskName would be a concatenation of a path and filename with a ".bak" suffix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM