标量值函数返回null

发布于 2025-02-13 06:56:23 字数 1297 浏览 0 评论 0 原文

我具有以下功能,对于我的一生,我无法让它恢复价值,我每次都会无效。

我正在通过选择[dbo]。[getfiatprofit](600.26,'gbp',1000.99,'btc')为op

我缺少什么?

/****** Object:  UserDefinedFunction [dbo].[getFiatProfit]    Script Date: 06/07/2022 11:42:26 ******/
   
    ALTER FUNCTION [dbo].[getFiatProfit] (
        @fiatInvested float,
        @fiatInvestedCurrency nvarchar,
        @quantity float,
        @currency nvarchar
    )
    RETURNS float
    AS
    BEGIN
        declare @tmp float
        declare @result float
        declare @usdtgbp float
        IF (@fiatInvestedCurrency = 'USD')
            BEGIN
                select @tmp = [dbo].[usdtPairs].[Value] from [dbo].[usdtPairs] where usdtPairs.ID = @currency;
                select @usdtgbp = [dbo].[usdtPairs].[Value] from [dbo].[usdtPairs] where usdtPairs.ID = 'GBP';
                set @result = (((@quantity * @tmp) - @fiatInvested) / @usdtgbp);
                -- set @result = @quantity * @tmp;
            END
        ELSE
            BEGIN
                select @tmp = [dbo].[usdtPairs].[Value] from [dbo].[usdtPairs] where usdtPairs.ID = @currency;
                set @result = ((@quantity * @tmp) - @fiatInvested);
                -- set @result = @quantity * @tmp;
            END
    return (@result)
    END

I have the below function, and for the life of me, I cannot get it to return a value, I get NULL every time.

I am calling it via select [dbo].[getFiatProfit](600.26,'GBP', 1000.99,'BTC') as op

What am I missing?

/****** Object:  UserDefinedFunction [dbo].[getFiatProfit]    Script Date: 06/07/2022 11:42:26 ******/
   
    ALTER FUNCTION [dbo].[getFiatProfit] (
        @fiatInvested float,
        @fiatInvestedCurrency nvarchar,
        @quantity float,
        @currency nvarchar
    )
    RETURNS float
    AS
    BEGIN
        declare @tmp float
        declare @result float
        declare @usdtgbp float
        IF (@fiatInvestedCurrency = 'USD')
            BEGIN
                select @tmp = [dbo].[usdtPairs].[Value] from [dbo].[usdtPairs] where usdtPairs.ID = @currency;
                select @usdtgbp = [dbo].[usdtPairs].[Value] from [dbo].[usdtPairs] where usdtPairs.ID = 'GBP';
                set @result = (((@quantity * @tmp) - @fiatInvested) / @usdtgbp);
                -- set @result = @quantity * @tmp;
            END
        ELSE
            BEGIN
                select @tmp = [dbo].[usdtPairs].[Value] from [dbo].[usdtPairs] where usdtPairs.ID = @currency;
                set @result = ((@quantity * @tmp) - @fiatInvested);
                -- set @result = @quantity * @tmp;
            END
    return (@result)
    END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻许诺言 2025-02-20 06:56:23

您的问题看起来是因为您的参数是没有长度的。 nvarchar 1 ,因此仅接收到错误的值。更好的数据类型将是 char(3),它是固定长度的,因为所有货币都有精确的三个字母的名称。

您还应该将此功能转换为表值函数,该功能可能会表现更好。

CREATE OR ALTER FUNCTION dbo.getFiatProfit (
    @fiatInvested float,
    @fiatInvestedCurrency char(3),
    @quantity float,
    @currency char(3)
)
RETURNS TABLE
AS RETURN

SELECT
  result = ((@quantity * u.Value) - @fiatInvested)
    / (CASE WHEN @fiatInvestedCurrency = 'USD'
        THEN 1
        ELSE
          (SELECT u2.Value FROM dbo.usdtPairs u2 WHERE u2.ID = 'GBP')
       END)
FROM dbo.usdtPairs u
WHERE u.ID = @currency;

你这样用它

SELECT t.*, fp.*
FROM YourTable t
CROSS APPLY dbo.getFiatProfit(t.fiatInvested, t.fiatInvestedCurrency, t.Qty, 'GBP') fp;

Your issue looks it's because your parameters are declared without a length. nvarchar defaults to a length of 1 in a lot of circumstances, so it's simply the wrong value being received. A much better data type would be char(3) which is fixed length, given that all currencies have exact three-letter names.

You should also convert this function into a Table Valued Function, which is likely to perform far better.

CREATE OR ALTER FUNCTION dbo.getFiatProfit (
    @fiatInvested float,
    @fiatInvestedCurrency char(3),
    @quantity float,
    @currency char(3)
)
RETURNS TABLE
AS RETURN

SELECT
  result = ((@quantity * u.Value) - @fiatInvested)
    / (CASE WHEN @fiatInvestedCurrency = 'USD'
        THEN 1
        ELSE
          (SELECT u2.Value FROM dbo.usdtPairs u2 WHERE u2.ID = 'GBP')
       END)
FROM dbo.usdtPairs u
WHERE u.ID = @currency;

You use it like this

SELECT t.*, fp.*
FROM YourTable t
CROSS APPLY dbo.getFiatProfit(t.fiatInvested, t.fiatInvestedCurrency, t.Qty, 'GBP') fp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文