首页 > 解决方案 > 如何使用插入和更新语句创建存储过程取决于条件?

问题描述

我有一个插入数据的存储过程,我需要更新存储过程并检查订单是否存在,然后更新,否则如果订单不存在,则插入新值。

这是我的存储过程,我需要这个想法来做,但我遇到了错误,我认为语法有错误:

ALTER PROCEDURE [dbo].[ADD_RESULT_COMMENT]
    @dept_id int,
    @order_id int,
    @comments varchar(1000),
    @comment_date datetime,
    @branch_id int,
    @testid int,
    @groupid int
AS
    SELECT comments 
    FROM Lab_Result_Comments 
    WHERE order_id = @order_id AND dept_id = @dept_id;

    IF comments IS NULL THEN 
    BEGIN
        INSERT INTO [dbo].[Lab_Result_Comments] ([dept_id], [order_id],[comments], [comment_date], branch_id, testid, groupid)
        VALUES (@dept_id, @order_id, @comments, @comment_date, @branch_id, @testid, @groupid)
    END;
    ELSE IF comments IS NOT NULL THEN 
    BEGIN
        UPDATE [dbo].[Lab_Result_Comments]
        SET [comments] = @comments
        WHERE order_id = @order_id
          AND dept_id = @dept_id
          AND Lab_Result_Comments.branch_id = @branch_id
 END;

我收到这些错误:

消息 156,级别 15,状态 1,过程 ADD_RESULT_COMMENT,第 12 行 [批处理开始第 7 行]
关键字“then”附近的语法不正确。

消息 156,级别 15,状态 1,过程 ADD_RESULT_COMMENT,第 19 行 [批处理开始第 7 行]
关键字“else”附近的语法不正确。

消息 156,级别 15,状态 1,过程 ADD_RESULT_COMMENT,第 19 行 [批处理开始第 7 行]
关键字“then”附近的语法不正确。

如何解决程序错误并修复并正确执行?

标签: sql-server

解决方案


如果你想选择一些东西然后检查它 - 你需要将它存储到一个变量中:

DECLARE @DbComments VARCHAR(1000);  -- or whatever your column is defined as 

SELECT @DbComments = comments 
FROM Lab_Result_Comments 
WHERE order_id = @order_id AND dept_id = @dept_id;

您需要确保SELECT只能返回一个- 否则您会收到其他错误....

此外:

  • 没有THEN- 只是放下那些
  • 如果要在IF块之后继续 -不要在之后使用分号END

试试这个代码:

IF @DbComments IS NULL    -- *NO* THEN here
BEGIN
    INSERT INTO ......
END                       -- *NO* semicolon here
ELSE 
BEGIN
    UPDATE [dbo].[Lab_Result_Comments] .......
END;

ELSE子句中的检查也不是必需的 - 只需将其关闭即可。

更新:好的,整个过程如下所示:

ALTER PROCEDURE [dbo].[ADD_RESULT_COMMENT]
    @dept_id int,
    @order_id int,
    @comments varchar(1000),
    @comment_date datetime,
    @branch_id int,
    @testid int,
    @groupid int
AS
    DECLARE @DbComments VARCHAR(1000);  -- or whatever your column is defined as 

    SELECT @DbComments = comments 
    FROM Lab_Result_Comments 
    WHERE order_id = @order_id AND dept_id = @dept_id;

    IF @DbComments IS NULL 
    BEGIN
        INSERT INTO [dbo].[Lab_Result_Comments] ([dept_id], [order_id],[comments], [comment_date], branch_id, testid, groupid)
        VALUES (@dept_id, @order_id, @comments, @comment_date, @branch_id, @testid, @groupid)
    END
    ELSE 
    BEGIN
        UPDATE [dbo].[Lab_Result_Comments]
        SET [comments] = @comments
        WHERE order_id = @order_id
          AND dept_id = @dept_id
          AND Lab_Result_Comments.branch_id = @branch_id
    END;

但正如@charlieface 提到并发布了 Aaron Bertrand 博客文章的链接 - 你可以像这样更容易地做到这一点:

ALTER PROCEDURE [dbo].[ADD_RESULT_COMMENT]
    @dept_id int,
    @order_id int,
    @comments varchar(1000),
    @comment_date datetime,
    @branch_id int,
    @testid int,
    @groupid int
AS
BEGIN
    -- just try to UPDATE the row
    UPDATE [dbo].[Lab_Result_Comments]
    SET [comments] = @comments
    WHERE order_id = @order_id
      AND dept_id = @dept_id
      AND Lab_Result_Comments.branch_id = @branch_id

    -- check if update was unsuccessful --> row doesn't exist yet, so insert
    IF @@ROWCOUNT = 0
    BEGIN
        INSERT INTO [dbo].[Lab_Result_Comments] ([dept_id], [order_id],[comments], [comment_date], branch_id, testid, groupid)
        VALUES (@dept_id, @order_id, @comments, @comment_date, @branch_id, @testid, @groupid)
    END;
END;

推荐阅读