TSQL Dynamic Update of TOP(n) Rows

会有一股神秘感。 提交于 2021-02-20 05:18:49

问题


i have a table with one line per travel second table has values of origin and destination and quantity for each combination i need to run an update that run on the second table that update top N rows on the first table (n comes from the second table)

im using this update script inside a cursor in a procedure :

ALTER PROCEDURE [dbo].[SP_Travels_History_FromMissing]

AS

BEGIN
    SET NOCOUNT ON;

    DECLARE @C_Day_Type_Code        INT = NULL
    DECLARE @C_PartOfDay_Code       INT = NULL
    DECLARE @C_Station_From         INT = NULL
    DECLARE @C_DestinationStation   INT = NULL
    DECLARE @C_Deploy               INT = NULL

    TRUNCATE TABLE  [TCK_TMP_Fact_ALL_Travels_History_FromMissing]
    INSERT
    INTO    [TCK_TMP_Fact_ALL_Travels_History_FromMissing]  
    SELECT  TRV.Day_Type_Code,
            TRV.PartOfDay_Code,
            HIS.Station_From,
            TRV.DestinationStation,
    --      TRV.TotalTravels,
    --      HIS.Percentage,
    --      (TRV.TotalTravels * HIS.Percentage) / 100,
            CAST(CEILING((TRV.TotalTravels * HIS.Percentage) / 100) AS INT)     Deploy
    FROM    
    (
        SELECT  DAT.Day_Type_Code,
    --          DATEPART(HOUR,[EntranceDate])   EntranceHour,
                POD.PartOfDay_Code,
                DestinationStation,
                COUNT(*)                        TotalTravels
        FROM    [Tickets].[dbo].[TCK_STG_Fact_Contract_Magnetic_Travels1]   TRV
        LEFT OUTER JOIN [Tickets].[dbo].[TCK_DWH_Dim_Date]                  DAT ON  CAST(TRV.[ExitDate] AS DATE) = CAST(DAT.Date AS DATE)
        LEFT OUTER JOIN [Tickets].[dbo].[TCK_DWH_Dim_Manual_PartOfDay]      POD ON  DATEPART(HOUR,TRV.[ExitDate]) = POD.PartOfDay_Hour
        WHERE   [EventID_Entrance] = -1
        GROUP BY    DAT.Day_Type_Code,
                    POD.PartOfDay_Code,
                    DestinationStation
    )   TRV
    LEFT OUTER JOIN
    (
        SELECT  *
        FROM    [dbo].[TCK_DWH_Fact_ALL_Travels_History_FromMissing]
        WHERE   [Total_Travels] IS NOT NULL
    )                                                                       HIS ON  HIS.Day_Type_Code = TRV.Day_Type_Code
                                                                                AND HIS.PartOfDay_Code = TRV.PartOfDay_Code
                                                                                AND HIS.Station_To = TRV.DestinationStation
    WHERE   CAST(CEILING((TRV.TotalTravels * HIS.Percentage) / 100) AS INT) > 0
    ORDER BY    1,2,4,5 DESC

    DECLARE Missing_Cursor CURSOR   FOR
    SELECT  [Day_Type_Code],
            [PartOfDay_Code],
            DestinationStation,
            Station_From,
            [Deploy]
    FROM    [Tickets].[dbo].[TCK_TMP_Fact_ALL_Travels_History_FromMissing]
    ORDER BY    [Day_Type_Code],
                [PartOfDay_Code],
                DestinationStation,
                [Deploy]    DESC,
                Station_From

    OPEN Missing_Cursor

    FETCH NEXT
    FROM    Missing_Cursor
    INTO    @C_Day_Type_Code,
            @C_PartOfDay_Code,
            @C_DestinationStation,
            @C_Station_From,
            @C_Deploy
    
    WHILE @@FETCH_STATUS = 0
    BEGIN

        UPDATE  TOP (@C_Deploy) TRV
        SET     TRV.SourceStationID = @C_Station_From,
                TRV.MissingStationSourceCode = 888
        FROM    [Tickets].[dbo].TCK_STG_Fact_Contract_Magnetic_Travels1 TRV
        LEFT OUTER JOIN [Tickets].[dbo].[TCK_DWH_Dim_Date]                  DAT ON  CAST(TRV.[ExitDate] AS DATE) = CAST(DAT.Date AS DATE)
        LEFT OUTER JOIN [Tickets].[dbo].[TCK_DWH_Dim_Manual_PartOfDay]      POD ON  DATEPART(HOUR,TRV.[ExitDate]) = POD.PartOfDay_Hour
        WHERE   DAT.Day_Type_Code = @C_Day_Type_Code
            AND POD.PartOfDay_Code = @C_PartOfDay_Code
            AND TRV.DestinationStation = @C_DestinationStation
            AND TRV.SourceStationID IS NULL

--      COMMIT;
--      ORDER BY    TRV.ContractID
--      LIMIT @C_Deploy;
-------------------------



        FETCH NEXT
        FROM    Missing_Cursor
        INTO    @C_Day_Type_Code,
                @C_PartOfDay_Code,
                @C_DestinationStation,
                @C_Station_From,
                @C_Deploy

    END

    CLOSE Missing_Cursor

    DEALLOCATE Missing_Cursor

END

Problem is It Takes more then 30 minutes

how can i use a dynamic update without a cursor? any other suggestions are welcome

来源:https://stackoverflow.com/questions/65196072/tsql-dynamic-update-of-topn-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!