以下是MIN函数使用的代码片段。执行下面的代码时会出错。
CREATE FUNCTION [dbo].[FN_TempCalcTransportExemp]
(
@EmployeeID varchar(20),
@PayElement varchar(20),
@Month varchar(10),
@FinYear varchar(10)
)
RETURNS decimal
AS
BEGIN
DECLARE @TarnsportExemption decimal(18,2) = 0
DECLARE @TDSIsFullExemption bit
DECLARE @PermanentPhysicalDisability decimal(18,2) = 0
DECLARE @UsingComapnyCar bit
DECLARE @Conveyance decimal(18,2) = 0
DECLARE @TransYes decimal(18,2) = 0
DECLARE @TransNo decimal(18,2) = 0
DECLARE @MinConveyance decimal(18,2) = 0
DECLARE @MinTransport decimal(18,2) = 0
SELECT
@TDSIsFullExemption = TDSDetailsFullExemption,
@TransYes = TDSDetailsYes,
@TransNo = TDSDetailsNo
FROM
tbl_TDSSettingDetails
WHERE
TDSSettingsDetailID = 2
SELECT
@Conveyance = @Month
FROM
tbl_Income
WHERE
EmployeeID = @EmployeeID
AND Element = @PayElement
AND FinancialYear = @FinYear
SELECT
@UsingComapnyCar = UsingCompanyCar,
@PermanentPhysicalDisability = PermanentPhysicalDisability
FROM
tbl_Employee_TDS
WHERE
EmployeeID = @EmployeeID
AND TDSFinancialYear = @FinYear
IF (@TDSIsFullExemption = 1)
BEGIN
SET @TarnsportExemption = @Conveyance
END
ELSE
BEGIN
IF (@UsingComapnyCar = 1)
BEGIN
IF (@Conveyance = 0)
BEGIN
SET @MinConveyance = 0
END
ELSE
BEGIN
SET @MinConveyance = @Conveyance
END
IF (@PermanentPhysicalDisability = 1)
BEGIN
SET @MinTransport = @TransYes
END
ELSE
BEGIN
SET @MinTransport = @TransNo
END
SET @TarnsportExemption = MIN(@MinConveyance, @MinTransport)
END
ELSE
BEGIN
SET @TarnsportExemption = 0
END
END
RETURN @TarnsportExemption
END
错误讯息:
Msg 174,Level 15,State 1,Procedure FN_TempCalcTransportExemp,Line 66个
MIN函数需要1个参数。
答案 0 :(得分:4)
set @TarnsportExemption = MIN(@MinConveyance,@MinTransport)
- MIN
函数不是您认为的。
你可能想做这样的事情:
set @TarnsportExemption = CASE WHEN @MinConveyance < @MinTransport THEN
@MinConveyance
ELSE
@MinTransport
END
另一种选择是:
SELECT @TarnsportExemption = MIN(val)
FROM
(
SELECT @MinConveyance as val
UNION ALL
SELECT @MinTransport as val
)
还有一个选择是使用values子句:
SELECT @TarnsportExemption = MIN(val)
FROM (VALUES ( @MinConveyance), (@MinTransport)) AS value(val)
答案 1 :(得分:0)
更改您的min语句,如下面MIN
。请参阅MIN (Transact-SQL)
--fROM
set @TarnsportExemption = MIN(@MinConveyance,@MinTransport)
--To
SELECT @TarnsportExemption = MIN(A) FROM (
SELECT @MinConveyance A
UNION ALL
SELECT @MinTransport
)AS AA
答案 2 :(得分:0)
在SQL中,MIN函数将返回所选列表中列的最小值;所以你只能在内联查询中使用MIN。
e.g。从Tbl_Employee
中选择min(Salary)
因此,在您的情况下,您可以使用case或者all all从两个变量中获取最小值: -
SET @TarnsportExemption = @MinConveyance&lt; @MinTransport那么@MinConveyance ELSE @MinTransport END
OR
SELECT @TarnsportExemption = MIN(TEMPS。[VALUE]) FROM( 选择@MinConveyance AS VALUE
UNION ALL
SELECT @MinTransport AS VALUE )AS TEMPS