在SQL中的存储过程中切换CASE

时间:2019-05-09 07:04:28

标签: sql-server

在开关盒中出现错误

SELECT CASE @PurchaseId
      WHEN 1 THEN INSERT into tblPurchaseInMstr(DocNo,DocType,DocDate,PurchaseDate,SalesNodeId,SalesNodeType,FYID,LoginIDIns, TimestampIns,StatusId,InvTotPurchValue,InvTotPurchWDisc,InvNetAmt,DocSourceNodeId,DocSourceNodeType,InvTotalCGSTAmt,InvTotalSGSTAmt,InvTotalIGSTAmt)
 values(@DocNo,@DocType,@DocDate,@PurchaseDate,@SalesNodeId,@SalesNodeType,@FYID,@LoginID, GETDATE(),@StatusId,@InvTotPurchValue,@InvTotPurchWDisc,@InvNetAmt,@DocSourceNodeId,@DocSourceNodeType,@InvTotalCGSTAmt,@InvTotalSGSTAmt,@InvTotalIGSTAmt)

WHEN @PurchaseId 2 THEN UPDATE tblPurchaseInMstr set DocNo=@DocNo DocType=@DocType,DocDate=@DocDate,PurchaseDate=@PurchaseDate,SalesNodeId=@SalesNodeId,SalesNodeType=@SalesNodeType,FYID=@FYID,LoginIDUpd=@LoginId,TimestampUpd=GETDATE(),StatusId=@StatusId,InvTotPurchValue=@InvTotPurchValue,InvTotPurchWDisc=@InvTotPurchWDisc,InvNetAmt=@InvNetAmt,DocSourceNodeId=@DocSourceNodeId,DocSourceNodeType=@DocSourceNodeType,InvTotalCGSTAmt=@InvTotalCGSTAmt,InvTotalSGSTAmt=@InvTotalSGSTAmt,InvTotalIGSTAmt=@InvTotalIGSTAmt where PurchaseId=@PurchaseId

 WHEN 3 THEN DELETE tblPurchaseInMstr

 END AS Purchaseid  from tblPurchaseInMstr

END

GO

2 个答案:

答案 0 :(得分:3)

您不能在CASE表达式中执行查询。一个case表达式是...好吧...只是一个应该产生值的表达式。

如果要基于变量/参数值执行语句,请使用IF块

并且我假设您不想在值3 ...的情况下删除整个表...)

编辑:

我刚刚在您的原始帖子中添加了一个带有问题的评论(关于@PurchaseId既是执行逻辑变量又是记录ID /键变量)。

假设@PurchaseId是一个标识列,我更新了第一个IF块以将插入的ID值提取到tblPurchaseInMstr.PurchaseId中。

然后我介绍了一个变量/参数@Action,它代表要执行的操作:1 =插入,2 =更新,3 =删除。

IF @Action = 1 BEGIN
    INSERT INTO tblPurchaseInMstr
    (
        DocNo,DocType,DocDate,PurchaseDate,SalesNodeId,SalesNodeType,FYID,LoginIDIns,
        TimestampIns,StatusId,InvTotPurchValue,InvTotPurchWDisc,InvNetAmt,DocSourceNodeId,
        DocSourceNodeType,InvTotalCGSTAmt,InvTotalSGSTAmt,InvTotalIGSTAmt
    )
    VALUES
    (
        @DocNo,@DocType,@DocDate,@PurchaseDate,@SalesNodeId,@SalesNodeType,@FYID,@LoginID,
        GETDATE(),@StatusId,@InvTotPurchValue,@InvTotPurchWDisc,@InvNetAmt,@DocSourceNodeId,
        @DocSourceNodeType,@InvTotalCGSTAmt,@InvTotalSGSTAmt,@InvTotalIGSTAmt
    );

    SET @PurchaseId = SCOPE_IDENTITY();
END;

IF @Action = 2 BEGIN
    UPDATE tblPurchaseInMstr
    SET
        DocNo=@DocNo, DocType=@DocType,DocDate=@DocDate,PurchaseDate=@PurchaseDate,SalesNodeId=@SalesNodeId,SalesNodeType=@SalesNodeType,FYID=@FYID,LoginIDUpd=@LoginId,
        TimestampUpd=GETDATE(),StatusId=@StatusId,InvTotPurchValue=@InvTotPurchValue,InvTotPurchWDisc=@InvTotPurchWDisc,InvNetAmt=@InvNetAmt,DocSourceNodeId=@DocSourceNodeId,
        DocSourceNodeType=@DocSourceNodeType,InvTotalCGSTAmt=@InvTotalCGSTAmt,InvTotalSGSTAmt=@InvTotalSGSTAmt,InvTotalIGSTAmt=@InvTotalIGSTAmt
    WHERE PurchaseId = @PurchaseId;
END;

IF @Action = 3 BEGIN
    DELETE FROM tblPurchaseInMstr
    WHERE PurchaseId = @PurchaseId;
END;

SELECT @PurchaseId AS PurchaseId;

答案 1 :(得分:0)

sql server中的

CASE是一个函数。它基于其他数据返回数据。它不接受语句,不控制程序流。

sql-server中没有switch。您只能使用IF

If @PurchaseId=1 insert into tblPurchaseInMstr(...
If @PurchaseId=2 UPDATE tblPurchaseInMstr...

以此类推。