0

我的子查询返回一个值,因为它是一个 MIN()。为什么我在调用函数时会收到以下错误?感谢 MS SQL Server 2008

子查询返回超过 1 个值。当子查询跟随时,这是不允许的......

Create Function dbo.ufnMinSpeed ( 
     @Track AS varchar(3),
     @Distance AS INT,
     @Surface  AS nchar(10),
     @MaxDate AS nchar (10)
) Returns Decimal(15,2) As
Begin

Declare @MinSpeed Decimal(15,2)

Return (
    Select Min (tr.Finaltime) Over (
        Partition By th.TrackCode, tr.Distance, tr.Surface
    ) AS MinSpeed
    From
        dbo.tblHeader th,
        dbo.tblRaceData tr,
        dbo.tblStarters ts
    Where
        th.TrackCode  = @Track And
        tr.Distance = @Distance And
        tr.Surface = @Surface And
        th.RaceDate < @MaxDate And
        tr.TrackDateRaceNumber = ts.TrackDateRaceNumber And
        th.TrackDateNumber = tr.TrackDateNumber
)

Return @MinSpeed

End
4

1 回答 1

0

我认为这纠正了评论中概述的所有问题。

Create Function dbo.ufnMinSpeed ( 
    @Track varchar(3),
    @Distance int,
    @Surface nchar(10),
    @MaxDate nchar (10)
) Returns Decimal(15,2) As
Begin
    Return (
        Select
            Min (tr.Finaltime)
        From
            dbo.tblHeader th
                Inner Join
            dbo.tblRaceData tr
                On th.TrackDateNumber = tr.TrackDateNumber
                Inner Join
            dbo.tblStarters ts
                On tr.TrackDateRaceNumber = ts.TrackDateRaceNumber
        Where
            th.TrackCode = @Track And
            tr.Distance = @Distance And
            tr.Surface = @Surface And
            th.RaceDate < @MaxDate
    )
End
于 2013-09-21T19:41:54.023 回答