sql-server - 如何使用插入和更新语句创建存储过程取决于条件?
问题描述
我有一个插入数据的存储过程,我需要更新存储过程并检查订单是否存在,然后更新,否则如果订单不存在,则插入新值。
这是我的存储过程,我需要这个想法来做,但我遇到了错误,我认为语法有错误:
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”附近的语法不正确。
如何解决程序错误并修复并正确执行?
解决方案
如果你想选择一些东西然后检查它 - 你需要将它存储到一个变量中:
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;
推荐阅读
- java - “没有主题替代 DNS 名称匹配
发现”使用 RestTemplate 时出错 - java - 子树JPQL的意外结束
- ssms - 为什么 17.8.1 版的 SSMS-setup-ENU 没有运行?
- php - 在php中从字符串中搜索数字
- python - python实例化一个嵌套类
- c# - Selenium C# - 尽管网站可以浏览,但 HttpWebRequest 始终返回 404
- javascript - 在 Javascript 中扩展对象
- javascript - 在括号之间指定参数和不指定参数有什么区别?
- dynamics-crm - 如何在 CRM Dynamics 中构建自定义页面?
- ios11 - 第一个表格单元格被 MFMessageComposeViewController 上的联系人输入字段覆盖