如何在SQL语句中使用IF ELSE

时间:2015-05-27 07:33:09

标签: sql if-statement sql-server-2012

我有一个存储过程,我正在尝试使用IF ELSE条件。

Alter procedure spInsertVirtualTransactions
(    
    @TerminalID varchar(8),    
    @Pan varchar(20),    
    @Product varchar(4)
)
AS

DECLARE @TERMINALNEWID VARCHAR(8)        

select @TERMINALNEWID=TERMINALID FROM TERMINAL WHERE TerminalID=@TerminalID  

Declare 
    @PlatinumLimitTaxi int = 4,
    @PlatinumLimitAirportLounge int = 4,
    @PlatinumLimitRoadAssistance int = 4,
    @GoldLimitTaxi int = 0,
    @GoldLimitAirportLounge int = 2,
    @GoldLimitRoadAssistance int = 0

Declare
    @CardTypeId int = (Select Card.CardTypeId From Card Where Card.EmbossLine=@PAN),
    @CardType int    

if (@CardTypeId=1)    
begin    
    Set @CardType=1    
end 
else 
begin
    if (@CardTypeId=2)
    begin
        Set @CardType=2
    End 
End
----------------------------------checking visits------------------------------------------------
Declare @VisitTaxi int = (Select SUM(Quantity) from POS_Transactions where (Product = '1' And PAN=@Pan))
Declare @VisitAirport int = (Select SUM(Quantity) from POS_Transactions where (Product = '2' And PAN=@Pan))


-----------------------------------taxi-----------------------------------------------------------
if (@Product ='1')
begin
    if (@CardType=1 and (@PlatinumLimitTaxi-@VisitTaxi) > 0)
    begin

         INSERT INTO pos_transactions        
         (TransactionDate, TerminalID, BatchNumber, SequenceNumber, Pan, TransactionTypeID, SettlementDate, Product, Quantity)
         VALUES        
         (GetDate(),@TERMINALNEWID, 0, -1, @Pan, 0, GETDATE(), @Product, 1)

         PRINT N'Platinum Taxi visits inserted.'
    end

    else    
    begin    
        Raiserror('Visits for Platinum Taxi have exceeded',16,1)    
        return    
    end

    if (@CardType=2 and (@GoldLimitTaxi-@VisitTaxi) > 0)    
    begin    
        INSERT INTO pos_transactions
        (TransactionDate, TerminalID, BatchNumber, SequenceNumber, Pan, TransactionTypeID, SettlementDate, Product, Quantity)       
        VALUES        
        (GetDate(), @TERMINALNEWID, 0, -1, @Pan, 0, GETDATE(), @Product, 1)

        PRINT N'Gold Taxi visits inserted.'
    end

    else    
    begin       
        Raiserror('Visits for Gold Taxi have exceeded the limit',16,1)    
        return      
    end
end

----------------------------------Airport-------------------------------------------------------
if (@Product='2')
begin
    if (@CardType=1 and (@PlatinumLimitAirportLounge-@VisitAirport) > 0)
    begin
        INSERT INTO pos_transactions        
        (TransactionDate, TerminalID, BatchNumber, SequenceNumber, Pan, TransactionTypeID, SettlementDate, Product, Quantity)       
        VALUES        
        (GetDate(), @TERMINALNEWID, 0, -1, @Pan, 0, GETDATE(), @Product, 1) 

        PRINT N'Platinum Airport visits inserted.'
    end

    else    
    begin

        Raiserror('Visits for Platinum Airport Lounge have exceeded',16,1)
        return

    end

    if (@CardType=2 and (@GoldLimitAirportLounge-@VisitAirport) > 0)
    begin
        INSERT INTO pos_transactions        
        (TransactionDate, TerminalID, BatchNumber, SequenceNumber, Pan, TransactionTypeID, SettlementDate, Product, Quantity)
        VALUES
        (GetDate(), @TERMINALNEWID, 0, -1, @Pan, 0, GETDATE(), @Product, 1)

        PRINT N'Gold Airport visits inserted.'
    end

    else    
    begin

        Raiserror('Visits for Gold Airport Lounge have exceeded the limit',16,1)
        return
    end
end 

假设我使用以下参数调用Stored Procedure

spInsertVirtualTransactions '00000001','22010402','2'

我应该得到

  

插入白金机场访问

但它也进入了第二个IF块,因此上面的消息我也得到了这个消息

  

黄金机场贵宾室的访问量已超出限额

这意味着它也会进入第二个IF块。我怎样才能改善这种状况。我可以使用ELSE IF吗?

1 个答案:

答案 0 :(得分:1)

在sql server中if..elseif..else的抱歉语法是

IF
Begin
....
END
ELSE IF
Begin
......
END
Else
...
END

In Your program

if (@Product ='1') 
    Begin
      if (@CardType=1) 
      Begin
         if (@PlatinumLimitTaxi-@VisitTaxi) > 0) 
         Begin
            ……..
         end
         else
         begin
                 ………….
         end
      end
     Else if (@CardType=2)
      begin
        if (@GoldLimitTaxi-@VisitTaxi) > 0) 
        Begin
         ………..
       end
        else
        begin
        ………
        end
      End
    End

----------------------------------Airport-------------------------------------------------------
if (@Product='2') 
Begin
    if (@CardType=1)
    Begin
      if (@PlatinumLimitAirportLounge-@VisitAirport) > 0)
      begin
      ……
     end
      Else
      begin
      ….
      End
   end

  Else if (@CardType=2)
  begin
      If (@GoldLimitAirportLounge-@VisitAirport) > 0)
      Begin
       …….
     end
     Else
     begin
       …..
      end
   end
end