我正在 sql server 中创建一个标量函数。并在函数中将列名作为参数传递。在这个函数中,我将这些列名与该表一起使用。这里@FromCurrency
和@ToCurrency
是CurrencyConversion
表的列名。
代码如下:
alter FUNCTION fnCurrencyConversion --1200.90,'NGN','CAD'
(@MoneyAmount MONEY,
@FromCurrency nvarchar(20),
@ToCurrency nvarchar(20))
RETURNS decimal(18,2)
AS
BEGIN
declare @BaseToUsd nvarchar(1000), @UsdToTarget nvarchar(1000)
declare @Rate decimal(18,5)
set @BaseToUsd= ('select top 1 '+ @FromCurrency +' from CurrencyConversion order by id desc')
set @UsdToTarget = ('select top 1 '+ @ToCurrency +' from CurrencyConversion order by id desc')
set @Rate = @UsdToTarget/@BaseToUsd
declare @TotalAmt decimal(18,5)
set @TotalAmt = (@Rate * @MoneyAmount)
return @TotalAmt
END
如何在sql中的标量值函数中传递列名?