首页 > 解决方案 > 为什么我的删除部分存储过程不起作用?

问题描述

我将存储过程用于插入和删除目的。我的插入工作正常,但我的删除部分不工作并且没有错误。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

这是我写的程序。这是我插入的表,也想执行删除

在此处输入图像描述

标签: sql-server

解决方案


您在 and 中使用相同的条件,将您IFELSE 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

推荐阅读