在存储过程中使用嵌套if

时间:2014-09-25 12:42:54

标签: sql-server-2005 stored-procedures

我正在尝试使用嵌套if创建存储过程,但我没有得到如何构建它。请帮帮我。 这就是我想要做的事情:

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

2 个答案:

答案 0 :(得分:0)

似乎您正在寻找AND operator ...

例如IF count=1 AND TotalGBV=25000 AND TotalPBV=200

答案 1 :(得分:0)

如果要检查多个条件,请在它们之间使用AND - 而不是逗号:

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