SQL Server:Tablockx附近的语法不正确。期待'('或选择

时间:2018-11-05 15:42:53

标签: sql-server tsql stored-procedures dynamic-sql

CREATE PROCEDURE [lot].[wipeAll] 
    @num VARCHAR(16),
    @quotaType INT,
    @TableName NVARCHAR(128)
AS
BEGIN
    SET NOCOUNT ON;

    DELETE FROM @TableName WITH (TABLOCKX) 
    WHERE num = @num AND quotaType = @quotaType;
END

我不知道如何解决此错误

  

Tablockx附近的语法不正确。期待'('或选择

上面的代码段中的

。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您需要为此使用动态SQL:

BEGIN
    SET NOCOUNT ON;

    DECLARE @sql NVARCHAR(MAX);

    SET @sql = '
DELETE FROM @TableName WITH (TABLOCKX) WHERE num=@num AND quotaType=@quotaType)';

    SET @sql = REPLACE(@sql, '@TableName', @TableName);

    EXEC sp_executesql @sql,
         '@num varchar(16), @quotaType int',
         @num=@num, @quotaType=quotaType
END;