If else in stored procedure sql server

后端 未结 8 1648
孤街浪徒
孤街浪徒 2021-02-02 07:19

I have created a stored procedure as follow:

Create Procedure sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY
(
@ParLngId int output
)
as
Begin
    SET @ParLngId = (Select         


        
8条回答
  •  后悔当初
    2021-02-02 07:34

    Try this with join query statements

    CREATE PROCEDURE [dbo].[deleteItem]
    @ItemId int = 0 
    AS
     Begin
     DECLARE @cnt int;
    
    SET NOCOUNT ON
    SELECT @cnt =COUNT(ttm.Id) 
        from ItemTransaction itr INNER JOIN ItemUnitMeasurement ium 
            ON itr.Id = ium.ItemTransactionId  INNER JOIN ItemMaster im 
            ON itr.ItemId = im.Id INNER JOIN TransactionTypeMaster ttm 
            ON itr.TransactionTypeMasterId = ttm.Id 
            where im.Id = @ItemId
    
    if(@cnt = 1)
        Begin
        DECLARE @transactionType varchar(255);
        DECLARE @mesurementAmount float;
        DECLARE @itemTransactionId int;
        DECLARE @itemUnitMeasurementId int;
    
            SELECT @transactionType = ttm.TransactionType,  @mesurementAmount = ium.Amount, @itemTransactionId = itr.Id, @itemUnitMeasurementId = ium.Id
            from ItemTransaction itr INNER JOIN ItemUnitMeasurement ium 
                ON itr.Id = ium.ItemTransactionId INNER JOIN TransactionTypeMaster ttm 
                ON itr.TransactionTypeMasterId = ttm.Id 
                where itr.ItemId = @ItemId  
            if(@transactionType = 'Close' and @mesurementAmount = 0)
                Begin
                    delete from ItemUnitMeasurement where Id = @itemUnitMeasurementId;
    
                End
            else
                Begin
                    delete from ItemTransaction where Id = @itemTransactionId;
                End
        End
    else
     Begin
        delete from ItemMaster where Id = @ItemId;
     End
    END
    

提交回复
热议问题