Sql server SQL Server标量UDF

Sql server SQL Server标量UDF,sql-server,Sql Server,我有一个带有多个if语句的udf。在当前表单中,它返回的结果不正确 以下是要求: 收入低于或等于235000的,不纳税 对于收入低于或等于335000的人,缴纳的税款等于(收入-235000)*10% 收入少于或等于410000的,应缴纳的税款等于(收入-335000)*20%+10000 对于41万以上的收入,如果收入小于或等于10000000,则应纳税25000+(收入-410000)*30%,对于10000000以上的收入,应纳税25000+(收入-410000)*0.3+(收入-100

我有一个带有多个if语句的udf。在当前表单中,它返回的结果不正确

以下是要求:

  • 收入低于或等于235000的,不纳税
  • 对于收入低于或等于335000的人,缴纳的税款等于
    (收入-235000)*10%
  • 收入少于或等于410000的,应缴纳的税款等于
    (收入-335000)*20%+10000
  • 对于41万以上的收入,如果收入小于或等于10000000,则应纳税
    25000+(收入-410000)*30%
    ,对于10000000以上的收入,应纳税
    25000+(收入-410000)*0.3+(收入-10000000)*0.1
这是我的UDF:

ALTER FUNCTION[dbo].[getPaye] (@grosspay MONEY)
RETURNS MONEY
AS
BEGIN
    -------------different income  bands
    DECLARE @lowerlimit MONEY = 215000
    DECLARE @midlimit MONEY = 335000
    DECLARE @upperlimit MONEY = 410000
    DECLARE @newupperlimit MONEY = 10000000

   --------------different percentages
   DECLARE @lowerpercent AS DECIMAL(3, 2) = 0.1
   DECLARE @midpercent AS DECIMAL(3, 2) = 0.2
   DECLARE @upperpecent AS DECIMAL(3, 2) = 0.3
   --------------------return value
   DECLARE @payeamount AS MONEY

   ----------------for incomes below 215,000 ..no tax paid
   IF @grosspay <= @lowerlimit
   BEGIN
       set @payeamount = 0
   END

   -------for income less than or equal to 335,000, tax the difference between the income and 215,000 at 10%
   IF @grosspay <= @midlimit
   BEGIN
       SET @payeamount = (@grosspay - @lowerlimit) * @lowerpercent
   END

   -------for Income less than or equal to 410,000, tax the difference between the incomes and 335,000 at 20% plus 10,0000
   IF @grosspay <= @upperlimit
   BEGIN
       SET @payeamount = 10000 + (@grosspay - @midlimit) * @midpercent
   END

   --- for income less than or equal 10,000,000 tax the difference between the income and 410,000 at 30% plus 25,000/=
   IF @grosspay >= @newupperlimit
   BEGIN
       SET @payeamount = 25000 + (@grosspay - @upperlimit) * @upperpecent        
   END

   -----for income above 10,000,000 tax than difference between the income and 410,000 at 30% plus 25,000 plus 10% *(Income-10,000,000)
   IF @grosspay > @newupperlimit
   BEGIN
       SET @payeamount = 25000 + (@grosspay - @upperlimit) * @upperpecent
                         + (@grosspay - @newupperlimit) * @lowerpercent
   END

   RETURN @payeamount
END
ALTER函数[dbo].[getPaye](@grosspay-MONEY)
还钱
作为
开始
-------------不同收入阶层
申报@lowerlimit MONEY=215000
申报@middlimit MONEY=335000
申报@upperlimitmoney=410000
声明@newupperlimit MONEY=10000000
--------------不同百分比
将@lowerpercent声明为十进制(3,2)=0.1
将@midpercent声明为十进制(3,2)=0.2
将@upperpecent声明为十进制(3,2)=0.3
--------------------返回值
将@payeamount声明为货币
----------------收入低于215000的,不纳税

如果@grosspay您的返回值被设置多次,因为您的条件不是独占的(并且您将返回值延迟到最后一次)

ALTER函数[dbo].[getPaye](@grosspay-money)
还钱
作为
开始
-------------不同收入阶层
申报@lowerlimit money=215000
申报@middlimit money=335000
申报@upperlimitmoney=410000
声明@newupperlimit money=10000000
--------------不同百分比
将@lowerpercent声明为十进制(3,2)=0.1
将@midpercent声明为十进制(3,2)=0.2
将@upperpecent声明为十进制(3,2)=0.3
----------------收入低于215000的,不纳税

如果@grosspay您的返回值被设置多次,因为您的条件不是独占的(并且您将返回值延迟到最后一次)

ALTER函数[dbo].[getPaye](@grosspay-money)
还钱
作为
开始
-------------不同收入阶层
申报@lowerlimit money=215000
申报@middlimit money=335000
申报@upperlimitmoney=410000
声明@newupperlimit money=10000000
--------------不同百分比
将@lowerpercent声明为十进制(3,2)=0.1
将@midpercent声明为十进制(3,2)=0.2
将@upperpecent声明为十进制(3,2)=0.3
----------------收入低于215000的,不纳税

如果@grosspayWhy结果不正确?你能提供一些样本数据和你期望的结果吗?另外,我还建议使用内联表值函数;它比标量函数快得多。问题可能在于每个
IF
都缺少
ELSE
RETURN
。所以你的条件并不是完全排他性的。当您已经知道每个if应该返回的值时,在每个if中进行返回。为什么结果不正确?你能提供一些样本数据和你期望的结果吗?另外,我还建议使用内联表值函数;它比标量函数快得多。问题可能在于每个
IF
都缺少
ELSE
RETURN
。所以你的条件并不是完全排他性的。当您已经知道每个if应该返回的值时,在每个if中执行一个返回。Ezlo的解决方案有效。我也学到了一些东西。非常感谢。罗纳尔德兹洛的解决方案奏效了。我也学到了一些东西。非常感谢。罗纳德
ALTER Function [dbo].[getPaye](@grosspay money)
returns money
as
begin
-------------different income  bands
Declare @lowerlimit money=215000
Declare @midlimit money=335000
Declare @upperlimit money=410000
declare @newupperlimit money=10000000
--------------different percentages
declare @lowerpercent as decimal(3,2)=0.1
declare @midpercent as decimal(3,2)=0.2
declare @upperpecent as decimal(3,2)=0.3
----------------for incomes below 215,000 ..no tax paid
     if @grosspay<=@lowerlimit
         begin
         RETURN 0
         end
-------for income less than or equal to 335,000, tax the difference between the income and 215,000 at 10%
     if @grosspay<=@midlimit
         begin
         RETURN (@grosspay-@lowerlimit)*@lowerpercent
         end
-------for Income less than or equal to 410,000, tax the difference between the incomes and 335,000 at 20% plus 10,0000
     if @grosspay<=@upperlimit
         begin
         RETURN 10000+(@grosspay-@midlimit)*@midpercent
         end
         ---for income less than or equal 10,000,000 tax the difference between the income and 410,000 at 30% plus 25,000/=
     if @grosspay<=@newupperlimit
         Begin
         RETURN 25000+(@grosspay-@upperlimit)*@upperpecent        
         end
     -----for income above 10,000,000 tax than difference between the income and 410,000 at 30% plus 25,000 plus 10% *(Income-10,000,000)
      RETURN 25000+(@grosspay-@upperlimit)*@upperpecent+(@grosspay-@newupperlimit)*@lowerpercent

end