当我尝试更改下面的功能时,我收到以下错误消息:
当选择列表中只能指定一个表达式时 子查询不是用EXISTS引入的。
我猜这可能是因为select into select。但是为什么这个选择单独选择(不在函数中)而不是在函数中。
ALTER FUNCTION [dbo].[Getcurrentexchangerate] (@CurrencyFromId INT,
@CurrencyToId INT)
returns DECIMAL(13, 10)
AS
BEGIN
DECLARE @rate DECIMAL (13, 10)
DECLARE @dw INT
SET @dw = (SELECT Datepart(dw, Getdate()))
IF( @dw != 2 ) -- Monday
BEGIN
SET @rate = (SELECT TOP (1) [rate]
FROM currencyconversionrate
WHERE [currencyfromid] = @CurrencyFromId
AND [currencytoid] = @CurrencyToId
ORDER BY id DESC)
END
ELSE
BEGIN
SET @rate = (SELECT *
FROM (SELECT TOP(2) Row_number()
OVER (
ORDER BY id DESC) AS
rownumber,
rate
FROM currencyconversionrate
WHERE ( [currencyfromid] = 2
AND [currencytoid] = 5 )
ORDER BY id DESC) AS Rate
WHERE rownumber = 2)
END
IF( @rate IS NULL )
BEGIN
SET @rate = 1
END
RETURN @rate
END
答案 0 :(得分:1)
查看“其他”部分
SET @rate = (SELECT *
FROM (SELECT TOP(2) Row_number()
OVER (
ORDER BY id DESC) AS
rownumber,
rate
FROM currencyconversionrate
WHERE ( [currencyfromid] = 2
AND [currencytoid] = 5 )
ORDER BY id DESC) AS Rate
WHERE rownumber = 2)
您尝试从currencyconversionrate表中选择所有字段,您不能这样做,或者您只想选择“RATE”列?
尝试更改为以下内容:
SET @rate = (SELECT rate
FROM (SELECT TOP(1) Row_number()
OVER (
ORDER BY id DESC) AS
rownumber,
rate
FROM currencyconversionrate
WHERE ( [currencyfromid] = 2
AND [currencytoid] = 5 )
ORDER BY id DESC) AS Rate
WHERE rownumber = 2)