简体   繁体   中英

SQL Server : incorrect syntax near Tablockx . Expecting '(' or Select

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

I can't figure out how to work around this error I'm getting

Incorrect syntax near Tablockx . Expecting '(' or Select

from the above code snippet. Any help would be greatly appreciated!

You need to use dynamic SQL for this:

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;

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