在标量变量周围添加单引号

时间:2013-11-07 04:47:08

标签: sql sql-server

我有一个存储过程,我试图执行一个@cmd变量,这是一个BACKUP数据库命令。当我运行存储过程(通过报告代理程序在作业中)时,我的路径中出现“C:”错误,我相信因为路径没有用引号括起来。我如何包装@Filename变量我必须有引号,以便数据库备份命令可以工作?

以下是存储过程的代码:

CREATE PROCEDURE pAgentBackup
AS
--1. Declare Variables.
DECLARE @MaxID INT
DECLARE @MinID INT
DECLARE @CurrentID INT
DECLARE @Path VARCHAR(100)
DECLARE @DBName VARCHAR(50)
DECLARE @Type CHAR(1)
DECLARE @cmd VARCHAR(500)
DECLARE @Recovery VARCHAR(300)
DECLARE @Filename VARCHAR(100)

SELECT @CurrentID = 0, @MaxID = MAX(BKID) FROM DBBackups

--2. Loop through the columns in the table and execute the backups.
WHILE @CurrentID < @MaxID
    BEGIN
        SELECT @CurrentID = MIN(BKID) FROM DBBackups WHERE BKID > @CurrentID

        SELECT @DBName = DbName FROM DBBackups WHERE BKID = @CurrentID
        SELECT @Path = Path FROM DBBackups WHERE BKID = @CurrentID
        SELECT @Type = Type FROM DBBackups WHERE BKID = @CurrentID

        IF @Type = 'F'
            BEGIN
                SELECT @Recovery = CAST(DATABASEPROPERTYEX(name, 'Recovery') AS VARCHAR(25))
                FROM master.dbo.sysdatabases WHERE name = @DBName

                --Set recovery to full if it is not already turned on.
                IF @Recovery <> 'FULL'
                    BEGIN
                        SELECT @cmd = 'ALTER DATABASE ' + @DBName + ' SET RECOVERY FULL'
                        EXEC (@cmd)
                    END

                --BEGIN Backup
                SET @Filename = @Path + @DBName + 'Full' +  CONVERT(VARCHAR(8), GETDATE(), 112) + '.bak'
                SET @cmd = 'BACKUP DATABASE ' + @DBName + ' TO DISK = ' + @Filename
                EXEC (@cmd)

                SET @Filename = @Path + '\Log' + @DBName + 'Log' +  CONVERT(VARCHAR(8), GETDATE(), 112) + '.bak'
                SET @cmd = 'BACKUP LOG ' + @DBName + ' TO DISK = ' + @Filename
                EXEC (@cmd)
            END

        IF @Type = 'L'
            BEGIN
                SELECT @Recovery = CAST(DATABASEPROPERTYEX(name, 'Recovery') AS VARCHAR(25))
                FROM master.dbo.sysdatabases WHERE name = @DBName

                --Set recovery to full if it is not already turned on.
                IF @Recovery <> 'FULL'
                    BEGIN
                        SELECT @cmd = 'ALTER DATABASE ' + @DBName + ' SET RECOVERY FULL'
                        EXEC (@cmd)
                    END

                --Begin Backup
                SET @Filename = @Path + '\Log' + @DBName + 'Log' +  CONVERT(VARCHAR(8), GETDATE(), 112) + '.bak'
                SET @cmd = 'BACKUP LOG ' + @DBName + ' TO DISK = ' + @Filename
                EXEC (@cmd)
            END
    END
GO

3 个答案:

答案 0 :(得分:0)

替换

SET @cmd = 'BACKUP LOG ' + @DBName + ' TO DISK = ' + @Filename

SET @cmd = 'BACKUP LOG ' + @DBName + ' TO DISK = ' + ''''+ @Filename +''''

答案 1 :(得分:0)

更改您的查询,如

 SET @cmd = 'BACKUP DATABASE ' + @DBName + ' TO DISK = ''' + @Filename + ''''

同时更改SP中使用的@Filename变量的所有查询。

答案 2 :(得分:0)

我认为REPLACE + QUOTENAME使查询更容易阅读和维护

SET @cmd = '
  BACKUP DATABASE @DBName 
  TO DISK = @Filename
';

SET @cmd = REPLACE(@cmd, '@DBName', QUOTENAME(@DBName));
SET @cmd = REPLACE(@cmd, '@Filename', QUOTENAME(@Filename, ''''));