' 0'附近的语法不正确。 (Microsoft SQL Server)

时间:2016-09-21 10:27:08

标签: sql-server

当我执行下面的程序时,它会返回Incorrect syntax near '0'。我不知道为什么。

错误已在Msg 102, Level 15, State 1, Line 43中进行了本地化。你能告诉我出了什么问题吗?谢谢你提前。

CREATE PROCEDURE [dbo].[PV_SaveCategory] 
(
    @AccountID              BIGINT,
    @LocationID             BIGINT,
    @CategoryName           NVARCHAR(100),
    @ParentID               BIGINT,
    @ExternalCategoryID     BIGINT
)
AS
BEGIN
DECLARE @ExecSQL NVARCHAR(MAX)

SET @ExecSQL = N'
DECLARE @CategoryID INT, @Level INT
SELECT @CategoryID = Id FROM [DB].[dbo].[Category_' + CONVERT(NVARCHAR, @AccountID, 0) + '] WHERE @ExternalCategoryID = ExternalID
SELECT @Level = Level+1 FROM [DB].[dbo].[Category_' + CONVERT(NVARCHAR, @AccountID, 0) + '] WHERE @ParentID = Id

IF @CategoryID IS NULL
BEGIN
INSERT
    INTO [DB].[dbo].[Category_' + CONVERT(NVARCHAR, @AccountID, 0) + ']
            (
                 Name
                ,LocationID
                ,IsDeleted
                ,Level
                ,ParentCategoryID
                ,Main
                ,SubCategory1
                ,SubCategory2
                ,SubCategory3
                ,SubCategory4
                ,CategoryPath
                ,ExternalID
            )
            VALUES
            (
                @CategoryName,
                @LocationID,
                0,
                CASE WHEN @ParentID IS NULL THEN 0 ELSE @Level END,
                @ParentID,
                0,
                1,
                2,
                4,
                ''test'',
                @ExternalCategoryID
            )
    END
ELSE
    BEGIN
        UPDATE [DB].[dbo].[Category_' + CONVERT(NVARCHAR, @AccountID, 0) + '] SET
                Level = CASE WHEN @ParentID IS NULL THEN 0 ELSE @Level END,
                0,
                1,
                2,
                3,
                4,
                ''test2'',
            Where @CategoryID = Id AND @ParentID = ParentCategoryID
    END'

    print @ExecSQL

    EXECUTE sp_executeSQl @ExecSQL, N'@AccountID BIGINT, @LocationID BIGINT, @CategoryName NVARCHAR(100), @ParentID BIGINT, @ExternalCategoryID BIGINT',
             @AccountID = @AccountID, @LocationID = @LocationID, @CategoryName = @CategoryName, @ParentID = @ParentID, @ExternalCategoryID = @ExternalCategoryID

END;

2 个答案:

答案 0 :(得分:4)

我看到了2个问题。

  1. 您在insert命令中定义了12个字段,只有11个值
  2. update语句没有列名(第一个除外)
  3. 它应该是这样的:

    UPDATE [DB].[dbo].[Category_' + CONVERT(NVARCHAR, @AccountID, 0) + '] 
    SET  Level = CASE WHEN @ParentID IS NULL THEN 0 ELSE @Level END,
         field1 = 0,
         field2 = 1,
         field3 = 2,
         field4 = 3,
         field5 = 4,
         field6 = 'test2'
     Where @CategoryID = Id 
     AND   @ParentID = ParentCategoryID
    

答案 1 :(得分:2)

在将值分配给其他缺少左侧列名称

之后,您的更新声明未完成