【问题标题】:User defined function to calculate differences between two columns based on value provided by user用户定义的函数,用于根据用户提供的值计算两列之间的差异
【发布时间】:2013-03-17 06:39:14
【问题描述】:

我的表中有四列:

  • 税收和其他支出百分比
  • *净额***
  • *Taxandotherexpendituremoney**
  • 金额

现在我想要的是 netamount 列中的用户定义函数,它计算 Amount 和根据用户提供的值之间的差异

即假设用户提供 25% 作为 Taxandotherexpenditurespercent,那么 NetAmount 必须是 amount-Taxandotherexpenditurespercent

同样,如果供应价值为 100,则 NetAmount 必须为 Amount-Taxandotherexpendituremoney

我已经尝试用谷歌搜索它和所有内容,但我找不到任何对用户定义函数的满意解释?

【问题讨论】:

    标签: sql-server-2008 user-defined-functions calculated-columns


    【解决方案1】:

    这是你可以做的。当然可以根据您的要求进行一些更改。

    CREATE FUNCTION UDF_FindDifference (@OtherExpenditures VARCHAR(50))
    RETURNS INT
    AS
    BEGIN
    DECLARE @NetAmount INT
    
    IF(CHARINDEX('%', @OtherExpenditures) > 0)
        BEGIN
    
            DECLARE @Amount INT
    
            SELECT @Amount = Taxandotherexpenditurespercent FROM yourTABLE
    
            SET @NetAmount = @Amount - REPLACE(@OtherExpenditures, '%', '')
    
    
        END
    ELSE
        BEGIN
    
            DECLARE @Amount2 INT
    
            SELECT @Amount2 = Taxandotherexpendituremoney FROM yourTABLE
    
            SET @NetAmount = @Amount2 - @OtherExpenditures
    
        END 
    RETURN @NetAmount
    END
    

    所以我在这里检查用户输入是否有% 符号,并根据这个标准进行计算。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多