sql-server - 为什么我的删除部分存储过程不起作用?
问题描述
我将存储过程用于插入和删除目的。我的插入工作正常,但我的删除部分不工作并且没有错误。SP 成功执行。
DECLARE
@UserID uniqueidentifier= '71019eed-af17-41ad-bea6-c27b0c21ad7d',
@CreatedBy varchar(100)= 'dhanil',
@CreatedDate datetime= NULL,
@UpdatedBy varchar(100)= NULL,
@UpdatedDate datetime= NULL,
@UserInProject nvarchar(max)= null,
@UserNotInProject nvarchar(max)= '0300d340-e2e4-452d-b77b-49830271c9bb,03a54a90-b7fb-465e-b057-7b876e117264,0f64aa12-1fab-4846-98c0-1d4589575eb4,18c9e7a8-d39d-435a-8d6a-718774239337'
declare @UserInValue uniqueidentifier=null,@UserNotInValue uniqueidentifier=null
IF(@UserInProject IS NOT NULL)
BEGIN
INSERT INTO PATS.UserInProject
SELECT NEWID(),@UserID,VALUE,@CreatedBy,@CreatedDate,NULL,NULL FROM STRING_SPLIT(@UserInProject,',')
END
ELSE IF(@UserInProject IS NOT NULL)
BEGIN
DELETE FROM PATS.UserInProject WHERE UserID=@UserID AND ProjectID IN (SELECT value FROM STRING_SPLIT(@UserNotInProject,','))
END
这是我写的程序。这是我插入的表,也想执行删除
解决方案
您在 and 中使用相同的条件,将您IF
的ELSE IF
更改ELSE IF
为
ELSE IF(@UserInProject IS NULL)
理想情况下,您不需要ELSE IF
,它可以简单地更改为ELSE
如果你想确保它@UserNotInProject
有值,你可以改变你的条件,如下所示。
IF(@UserInProject IS NOT NULL)
BEGIN
--INSERT
END
ELSE IF(@UserNotInProject IS NOT NULL)
BEGIN
--DELETE
END
推荐阅读
- postgresql - 从 Postgres 13 开始,哈希索引是否最适合精确文本匹配查询?
- java - 未在 JSP 中使用 setHeader 和 addCookie 创建 Cookie
- javascript - 我正在尝试显示我使用其类名定位的 Iframe 的某个部分
- git - 如何从 git 历史中提取模块到新的 repo
- python - 使用 Django 的每个订单行和全球的商品总价
- javascript - Javascript 中的循环是如何执行的?
- azure-data-factory - Azure DataFactory 将数据从一个数据库复制到另一个数据库,数据类型问题
- ruby-on-rails - 向 has_and_belongs_to_many 关系添加多条记录
- android - 在 android studios 上开发应用程序时是否有做笔记的约定
- python - 基于 Python 的 UDP 交叉开关多路复用器