Sql server 将函数转换为简单查询

Sql server 将函数转换为简单查询,sql-server,Sql Server,我有以下返回包裹延迟的函数: ALTER FUNCTION [dbo].[Delay] (@DeliveryTime int, @DeliveryHour int = 0, @Product_ID int, @Country varchar(2), @ZipCode varchar(10) ) RETURNS int AS BEGIN DECLARE @Delay int, @Guaranteed Int, @MaxDeliveryTime int, @MaxHour int, @test i

我有以下返回包裹延迟的函数:

ALTER FUNCTION [dbo].[Delay]
(@DeliveryTime int, @DeliveryHour int = 0, @Product_ID int, @Country varchar(2), @ZipCode varchar(10)
)
RETURNS int
AS
BEGIN

DECLARE @Delay int, @Guaranteed Int, @MaxDeliveryTime int, @MaxHour int, @test int

if @Country = 'FR'
    begin
        select  top 1 @Guaranteed = pz.TProduct_Guaranteed, 
                @MaxDeliveryTime = pz.TProduct_delay, 
                @MaxHour = pz.TProduct_HourMax
        from    TProductZone PZ 
        where   (pz.Country_ID = '0' OR pz.Country_ID = cast(@Country as varchar(2)))
                And (Zone_ID = '0' or Zone_ID = left(@ZipCode,2))
                And p.tproduct_id = @Product_ID
        Order by pz.country_id desc, zone_id desc
    end
Else
    Begin
        select  top 1 @Guaranteed = pz.TProduct_Guaranteed, 
                @MaxDeliveryTime = pz.TProduct_delay, 
                @MaxHour = pz.TProduct_HourMax
        from    TProductZone PZ 
        where   (pz.Country_ID = '0' OR pz.Country_ID = cast(@Country as varchar(2)))
                And (Zone_ID = '0')
                And p.tproduct_id = @Product_ID
        Order by pz.country_id desc, zone_id desc
    End

if @Guaranteed = 1
    Begin
        if @DeliveryTime >= @MaxDeliveryTime
            Begin
                set @Delay = @DeliveryTime-@MaxDeliveryTime
                if @DeliveryHour > @MaxHour
                    begin
                        set @Delay = @Delay+1
                    end
            End
        else
            Begin
                set @Delay = 0
            end
    End
Else
    Begin
        set @Delay = 0
    End 

return @Delay

END
此功能的用途如下:

Update  TShipping
    Set     Delay = Delay(S.TShipping_Deliverytime, cast(left(isnull(TD.Tracking_Hour,0),2) as int), S.TProduct_ID, S.Country_Code, S.ZipCode)
    From    TShipping S
            inner Join TrackingEndDelay TD On TD.TShipping_ID = S.TShipping_ID
    WHERE   IsNull(TShipping_Delivered,0) = 1
            And DateDiff(day,TShipping_Date,getdate()) < 90
我想在一个独特的简单查询中转换这个函数和函数调用,以减少响应时间

这就是我的疑问:

Update  TShipping
    Set     Delay =
case 
    WHEN TProduct_Guaranteed=0 or (TShipping_Deliverytime - TProduct_delay < 0) then 0
    WHEN (TShipping_Deliverytime - TProduct_delay >= 0) and cast(left(isnull(TD.Tracking_Hour,0),2) as int)  <= isnull(TProduct_HourMax,24) then TShipping_Deliverytime - TProduct_delay
    when (TShipping_Deliverytime - TProduct_delay >= 0) and cast(left(isnull(TD.Tracking_Hour,0),2) as int)  > isnull(TProduct_HourMax,24) then TShipping_Deliverytime - TProduct_delay +1
end
from    tshipping TS 
inner Join TrackingEndDelay TD On TD.TShipping_ID = TS.TShipping_ID
left join dbo.TShipping_DateInDelivery DID on ts.tshipping_id=DID.tshipping_id 
 outer apply  (select   top 1 pz.TProduct_Guaranteed, pz.TProduct_delay, pz.TProduct_HourMax
        from    TShipping S
                inner join TProductZone PZ on S.tproduct_id = pz.tproduct_id
        where   (pz.Country_ID = '0' OR pz.Country_ID = S.Country_Code)
                And (Zone_ID = '0' or (Zone_ID = left(S.TShipping_ZipCode,2) and S.Country_Code='FR'))
                and TS.TShipping_ID=S.TShipping_ID
        Order by pz.country_id desc, zone_id desc) r1
WHERE   IsNull(TShipping_Delivered,0) = 1
And DateDiff(day,TShipping_Date,getdate()) < 90
问题是我增加了响应时间,而不是减少它。有没有其他更好的方法更快地完成任务


谢谢你的帮助

与其将所有代码都粘贴到文章中,不如添加一些示例数据以及所需的输出和用于创建它的基本逻辑。目前,很少有人愿意在建议替代方案之前仔细研究您的SQL以了解它在做什么。请提供示例数据