0

I'm trying to accomplish the IF ELSE statement in a SQL Server stored procedure but it seems that it won't follow the condition. I tried to declare a static value for me to check it but its still the same. My problem is it wont go to ELSE even if the condition is wrong

Here's the code:

ALTER PROCEDURE [dbo].[Amount_Computation]
    (@OfficeID int,
     @AccountID int,
     @Amount int,
     @NoOfMonths int,
     @Percentage int,
     @isRoundOf int,
     @MaxAmount int,
     @EmployeeType int)
AS
    declare @TotalAmount table(TotalAmount int)
    declare @Casual table(CasualSalary int, OfficeID int)
    declare @Regular table(RegularSalary int, OfficeID int)

    declare @basic int
BEGIN
    SELECT 
        @Amount = 1, @OfficeID = 72,
        @AccountID = 733, @Amount = 0, 
        @NoOfMonths = 12, @Percentage = 1.25, 
        @isRoundOf = 1, @MaxAmount = 35000, 
        @EmployeeType = 1

    IF(@Amount = 0)
    BEGIN
         INSERT INTO @Casual 
             SELECT 
                 CAST(((select LEFT(CONVERT(nvarchar,CAST((Case when (basic * 22 > @MaxAmount) then @MaxAmount ELSE Basic * 22 END) AS INT)),LEN(CONVERT(nvarchar,CAST((Case when (basic * 22 > @MaxAmount) then @MaxAmount ELSE Basic * 22 END) AS INT))) - 3)) + '000' ) AS INT ) * @Percentage / 100 * @NoOfMonths as Casual, 
                 a.OfficeID 
             FROM 
                 pmis.dbo.vw_RGPermanentAndCasual as a 
             LEFT JOIN 
                 ifmis.dbo.tbl_R_BMSOffices as b ON b.PMISOfficeID = a.OfficeID 
             WHERE 
                 a.OfficeID = @OfficeID AND a.EmploymentGroup = '2'

        INSERT INTO @Regular 
            SELECT 
                CAST(((select LEFT(CONVERT(nvarchar,CAST((Case when (basic > @MaxAmount) then @MaxAmount ELSE Basic END) AS INT)),LEN(CONVERT(nvarchar,CAST((Case when (basic > @MaxAmount) then @MaxAmount ELSE Basic END) AS INT))) - 3)) + '000' ) AS INT ) * @Percentage / 100 * @NoOfMonths as Regular, 
                a.OfficeID 
            FROM 
                pmis.dbo.vw_RGPermanentAndCasual as a 
            LEFT JOIN 
                ifmis.dbo.tbl_R_BMSOffices as b ON b.PMISOfficeID = a.OfficeID 
            WHERE 
                a.OfficeID = @OfficeID AND a.EmploymentGroup = '1'

        INSERT INTO @TotalAmount 
            SELECT 
                SUM(CasualSalary) + SUM(RegularSalary) 
            FROM 
                @Casual as a 
            LEFT JOIN 
                @Regular as b ON b.OfficeID = a.OfficeID
    END
    ELSE IF(@Amount = 1)
    BEGIN
        INSERT INTO @TotalAmount 
            SELECT SUM(CasualSalary) as ELSE_IF 
            FROM @Casual
    END
END

/**SELECT CasualSalary FROM @Casual
SELECT RegularSalary FROM @Regular **/
SELECT TotalAmount FROM @TotalAmount

EDIT: Select @Amount should be

SELECT @Amount = 1,@OfficeID = 72,@AccountID = 733, @NoOfMonths = 12, @Percentage = 1.25, @isRoundOf = 1, @MaxAmount = 35000, @EmployeeType = 1
4
  • 3
    Code looks correct. How do you know ELSE doesn't run? Can you add a PRINT 'test' in ELSE to check? Commented May 31, 2016 at 2:42
  • @CoolBots I tried your suggestion. I Put PRINT 'TEST IF' and PRINT 'ELSE IF' and the result is still the same. The output is TEST IF Commented May 31, 2016 at 2:50
  • @CoolBots I already solved the problem.. The thing is I double initialize the @Amount. Thanks Commented May 31, 2016 at 2:53
  • Wow, I.just saw that, it's twice in your SELECT list! Nice, easy fix! Commented May 31, 2016 at 2:55

1 Answer 1

1

As you noticed yourself - @Amount is listed twice in your SELECT list, with two distinct values.

Easy fix. :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.