为什么我的“删除存储过程”部分不起作用?

时间:2019-02-20 10:59:14

标签: sql-server

我将存储过程用于插入和删除目的。我的插入工作正常,但是“删除”部分不起作用,并且没有任何错误。 S.P成功执行。

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

这是我编写的过程。 这是我在其中插入并且还想执行Delete

的表

enter image description here

2 个答案:

答案 0 :(得分:3)

您在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

答案 1 :(得分:0)

此代码可能有效。

    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,',')
        DELETE FROM PATS.UserInProject WHERE UserID=@UserID AND ProjectID IN (SELECT value FROM STRING_SPLIT(@UserNotInProject,','))
    END