标量值函数返回null
我具有以下功能,对于我的一生,我无法让它恢复价值,我每次都会无效。
我正在通过选择[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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题看起来是因为您的参数是没有长度的。
nvarchar
1 ,因此仅接收到错误的值。更好的数据类型将是char(3)
,它是固定长度的,因为所有货币都有精确的三个字母的名称。您还应该将此功能转换为表值函数,该功能可能会表现更好。
你这样用它
Your issue looks it's because your parameters are declared without a length.
nvarchar
defaults to a length of1
in a lot of circumstances, so it's simply the wrong value being received. A much better data type would bechar(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.
You use it like this