【问题标题】:using nested if in stored procedure在存储过程中使用嵌套 if
【发布时间】:2014-09-25 12:42:54
【问题描述】:

我正在尝试使用嵌套 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

【问题讨论】:

  • 你能给出一些示例数据和预期的结果吗?看起来您正在做的事情可能可以在单个 UPDATE 语句中制定,而不是先执行测试然后应用多个更新。另外,我怀疑您的很多UPDATEs 缺少WHERE 子句。
  • 感谢您的回答,我收到以下错误:关键字“if”附近的语法不正确。在预期条件的上下文中指定的非布尔类型表达式,靠近 ')'。

标签: sql-server-2005 stored-procedures


【解决方案1】:

您似乎在寻找AND operator...

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

【讨论】:

    【解决方案2】:

    如果您想检查多个条件,请在它们之间使用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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 2011-09-28
      相关资源
      最近更新 更多