using nested if in stored procedure

2019-08-17 08:18发布

问题:

I am trying to create a stored procedure using nested if, but I am not getting how to frame it. Please help me out. This if what I am trying to do:

CREATE PROCEDURE StarDistributorProfit 
@sponsorId varchar(20),

AS
if exists(select sponsor_id where(select count(user_id) from usertransaction where bv=50001))
    if count=1,TotalGBV=25000,TotalPBV=200
BEGIN
update usertransaction set rank='executive' where sponsor_id=@sponsorId
update usertransaction set dp=(0.309*BV)
update usertransaction set leadership_bonus=(0.07*BV)
END
if count=2,TotalGBV=20000,TotalPBV=200
BEGIN
update usertransaction set rank='star executive' where sponsor_id=@sponsorId
update usertransaction set dp=(0.318*BV)
update usertransaction set leadership_bonus=(0.03*BV)
END
if count=3,TotalGBV=20000,TotalPBV=300
BEGIN
update usertransaction set rank='Organizer' where sponsor_id=@sponsorId
update usertransaction set dp=(0.318*BV)
update usertransaction set leadership_bonus=(0.03*BV)
END 
GO

回答1:

Seems you are looking for AND operator...

For example IF count=1 AND TotalGBV=25000 AND TotalPBV=200.



回答2:

If you want to check multiple conditions, use AND between them - not a comma:

IF count = 1 AND TotalGBV = 25000 AND TotalPBV = 200
BEGIN
   UPDATE usertransaction SET rank = 'executive' WHERE sponsor_id = @sponsorId
   UPDATE usertransaction SET dp = (0.309 * BV)
   UPDATE usertransaction SET leadership_bonus = (0.07*BV)
END
-- and so forth for all your IF^s