4

我不是 SQL 专家,我正在编写一个庞大的 SQL 代码,不幸的是,它有一个 CURSOR 处理其中的另外两个嵌套 CURSORS(存储过程中总共三个游标),它处理数百万要删除的数据,更新和插入。

由于逐行执行,这需要大量时间(超过 13 小时并最终得到错误“光标名称数据集已存在”),我正在尝试将其修改为基于 SET 的方法,但我被卡住了在转换为基于 SET 的方法时执行 INSERT、DELETE 和 UPDATE。或者换句话说,我不能通过在 JOINS 中替换它们来完成所有的 INSERT、DELETE 和 UPDATE。

我知道这是一个大代码,可能不适合在这里发布,但我已经以一种简单的方式将它放在另一篇帖子中,人们不明白我的问题,所以我把实际代码放在这里。

这是我正在处理的代码:

ALTER PROCEDURE [dbo].[proc_Upload]  WITH RECOMPILE 
as
set NoCount on

DECLARE
    @StartTime      datetime,
    @EndTime        datetime,
    @DataID         uniqueidentifier,
    @CollectionDate datetime,
    @Status         int,
    @PeriodID       int,
    @EndDate        datetime,
    @GroupID        uniqueidentifier,
    @ProjectID      INT,
    @FAID           uniqueidentifier,
    @UploadID       int ,
    @Createdate datetime


declare @projects table(
    [ProjectID] [INT],
    [Title] [varchar](255) ,
    [currency] [int] ,
    [Cost_Until_Today] [float] ,
    [StartDate] [datetime] ,
    [EndDate] [datetime] ,
    [MisID] [uniqueidentifier] ,
    [SystemStatus] [int] ,
    [FacilitatingAgency] [uniqueidentifier] ,
    [SyncMode] [int] )

 INSERT @projects
 SELECT ID, PROJECTNAME, PROJECTCURRENCY_ID, Cost_Until_Today, PROJECTESTABLISHEDDATE, EndDate, MisID, 4, FacilitatingAgency, SyncMode 
 FROM [dbo].[T_PROJECTS] /*thsi is the source table where every projectIDs need to be processed*/



 IF exists ( select  *
            from    dbo.sysobjects
            where   id = object_id(N'[dbo].[datasets]') and objectproperty(id, N'IsTable') = 1 ) 
    DROP Table [dbo].datasets


    SELECT mgd.Gd_ID, mg.Grp_ID, mgd.Gd_CollectionDate, mgd.Gd_IsDeleted, mgd.Gd_CreateDate, mg.Grp_Project, mg.Grp_IsDeleted , mg.Grp_Legacy_ID,  p.LegacyProjectId
    INTO datasets
    FROM Savix_Service_Group..Group_Data mgd 
            INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_ID = mgd.Gd_CycleID
            INNER JOIN Savix_Service_Group..Groups mg ON mg.Grp_ID = mgc.Gc_GroupID 
            inner join SG_Dynamic_Forms..v_projects p ON p.ProjectID = mg.Grp_Project


DECLARE projects1 CURSOR LOCAL FOR Select distinct ProjectID, isnull([FacilitatingAgency], '00000000-0000-0000-0000-000000000000') from @projects P WHERE P.SystemStatus = 4 AND P.SyncMode = 1 
/*First cursor - fetch the cursor from ProjectaTable*/


OPEN projects1
FETCH NEXT FROM projects1 INTO @ProjectID, @FAID
WHILE @@FETCH_STATUS = 0
BEGIN
    BEGIN TRY
        BEGIN TRAN

        DELETE FROM T_PROJECTGROUPSDATA
            WHERE T_PROJECTGROUPSDATA.UPLOAD_ID IN (SELECT ID FROM T_UPLOADS WHERE project_savix_ID = @ProjectID AND UPLOADFILENAME = 'Automatic upload from web MIS')

        DECLARE datasets CURSOR LOCAL  FAST_FORWARD FOR SELECT Gd_ID, Grp_ID, Gd_CollectionDate, Gd_IsDeleted, Gd_CreateDate 
            FROM datasets
            WHERE LegacyProjectId = @ProjectID AND Grp_IsDeleted = 0 AND Gd_IsDeleted != 1 
            /*Second cursor - this will get the 'collectionDate'field from datasetsTable for every project fetched in above cursor and also get @dataID which is used to insert value in to other table-T_PROJECTGROUPSDATA*/
        OPEN datasets

        FETCH NEXT FROM datasets INTO @DataID, @GroupID, @CollectionDate, @Status, @Createdate
        WHILE @@FETCH_STATUS = 0
        BEGIN
            DECLARE period CURSOR LOCAL  FAST_FORWARD FOR SELECT ID, dbo.fn_GetEndOfPeriod(ID) FROM T_PERIODS
            /* dbo.fn_GetEndOfPeriod(ID) - this function will give the end of the date of that specifc quarter for any given date*/
                WHERE DATEDIFF(dd,@CollectionDate,dbo.fn_GetEndOfPeriod(ID)) >= 0
                ORDER BY [YEAR],[Quarter]
                /*Third Cursor - this will process the records from another table called period with above fetched @collectionDate*/

            OPEN period
            FETCH NEXT FROM period INTO @PeriodID, @EndDate
            WHILE @@FETCH_STATUS = 0
            BEGIN

                IF EXISTS (SELECT * FROM Savix_Service_Group..Group_Data mgd 
                    INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_ID = mgd.Gd_CycleID
                    WHERE mgc.Gc_GroupID = @GroupID
                        AND DATEDIFF(dd,mgd.Gd_CollectionDate,@EndDate) >= 0 
                        AND (mgd.Gd_CollectionDate > @CollectionDate ) 
                        AND mgd.Gd_IsDeleted != 1) 

                BEGIN
                    BREAK
                END

                IF EXISTS (SELECT ID FROM T_UPLOADS u 
                    WHERE  u.project_savix_ID = @ProjectID AND u.PERIOD_ID = @PeriodID AND u.STATUS = 3 AND UPLOADFILENAME != 'Automatic upload from web MIS') 
                BEGIN
                    FETCH NEXT FROM period INTO @PeriodID, @EndDate
                    CONTINUE
                END

                SET @UploadID = (SELECT ID FROM T_UPLOADS u WHERE  u.project_savix_ID = @ProjectID AND u.PERIOD_ID = @PeriodID AND u.STATUS = 3)

                /*If T_uploads doesn't have appropirate period ID from cursor fetch then create a new entry in T_uploads with current projectID*/

                IF @UploadID IS NULL
                        BEGIN

                            declare @Project_ID_Legacy int = ISNULL((select distinct PROJECT_ID from T_UPLOADS where project_savix_ID = @ProjectID),@ProjectID)

                                INSERT INTO T_UPLOADSFIRSTSTEP
                                       (PROJECT_ID
                                       ,UPLOADDATE
                                       ,UPLOADFILENAME
                                       ,UPLOADUSER_ID
                                       ,CURRENTSTEP
                                       ,STATUS
                                       ,Project_ID_MIS)
                                 SELECT @Project_ID_Legacy , --mm 06/12
                                        GETDATE(),
                                        'Automatic upload from web MIS',
                                        2,
                                        2,
                                        0,
                                        @ProjectID

                                INSERT INTO T_UPLOADS
                                       (ID, periodID, projectID,UPLOADDATE,UPLOADFILENAME,UPLOADUSER_ID )
                                 SELECT uf.ID,
                                        @PeriodID,
                                        uf.PROJECT_ID,
                                        uf.UPLOADDATE,
                                        uf.UPLOADFILENAME,
                                        uf.UPLOADUSER_ID

                                 FROM T_UPLOADSFIRSTSTEP uf
                                 INNER JOIN @projects mp ON  uf.Project_ID_MIS = mp.ProjectID
                                 WHERE  uf.Project_ID_MIS = @ProjectID AND uf.STATUS = 0 
                                    AND NOT EXISTS (SELECT * FROM T_UPLOADS u WHERE u.PROJECT_ID  = uf.PROJECT_ID AND u.PERIOD_ID = @PeriodID AND u.STATUS = 3)

                                DELETE FROM T_UPLOADSFIRSTSTEP WHERE STATUS = 0 AND PROJECT_ID = @Project_ID_Legacy 

                                SET @UploadID = (SELECT ID FROM T_UPLOADS u WHERE  u.project_savix_ID = @ProjectID AND u.PERIOD_ID = @PeriodID AND u.STATUS = 3)
                END ELSE
                            UPDATE T_UPLOADS SET 
                                TOTALEXPENDITURES = CASE WHEN DATEDIFF(d,mp.StartDate,mp.EndDate) != 0 
                                            THEN mp.Cost_Until_Today*DATEDIFF(d,mp.StartDate,dbo.fn_GetEndOfPeriod(@PeriodID))/DATEDIFF(d,mp.StartDate,mp.EndDate)/dbo.fn_RateAtDate(mp.Currency,dbo.fn_GetEndOfPeriod(@PeriodID))
                                        ELSE 0 END,
                                TotalExpendituresNative = CASE WHEN DATEDIFF(d,mp.StartDate,mp.EndDate) != 0 
                                            THEN mp.Cost_Until_Today*DATEDIFF(d,mp.StartDate,dbo.fn_GetEndOfPeriod(@PeriodID))/DATEDIFF(d,mp.StartDate,mp.EndDate)
                                        ELSE 0 END
                            FROM @projects mp
                            WHERE T_UPLOADS.ID = @UploadID AND mp.ProjectID = @ProjectID

                        INSERT INTO T_PROJECTGROUPSDATA 
                                (uploadID, fieldA,fieldB,......fieldN )

                         SELECT @UploadID,p.fieldA,mg.fieldB,......mgc.fieldN
                         FROM @projects mp
                         inner join  SG_Dynamic_Forms..v_projects p ON p.LegacyProjectId = mp.projectID 
                         inner join Savix_Service_Group..Groups mg ON mg.Grp_Project = p.ProjectID 
                         INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_GroupID = mg.Grp_ID
                         INNER JOIN Savix_Service_Group..Group_Data mgd ON mgd.Gd_CycleID = mgc.Gc_ID
                         LEFT JOIN Savix_Service_Trainers..Trainers me ON me.Tr_ID = mgc.Gc_MonitoredBy
                         LEFT JOIN Savix_Service_Dictionaries..Dictionary mgt ON mgt.Dny_ID = me.Tr_Type 
                         --left join v1_Report_UDF_Data_UploadToSavix udf on udf.DataID = mgd.Gd_ID
                         WHERE mgd.Gd_ID = @DataID 

                FETCH NEXT FROM period INTO @PeriodID, @EndDate
            END
            CLOSE period
            DEALLOCATE period

            FETCH NEXT FROM datasets INTO @DataID, @GroupID, @CollectionDate, @Status, @Createdate
        END

        CLOSE datasets
        DEALLOCATE datasets

        COMMIT
    END TRY
    BEGIN CATCH



        SELECT ERROR_NUMBER(), ERROR_MESSAGE(),@PeriodID, @ProjectID, @UploadID,@DataID
        IF CURSOR_STATUS('global' , 'period') >= 0
        BEGIN
            CLOSE period
            DEALLOCATE uploadID
        END

        IF CURSOR_STATUS('global' , 'datasets') >= 0
        BEGIN
            CLOSE datasets
            DEALLOCATE datasets
        END

        IF @@TRANCOUNT > 0
            ROLLBACK

        INSERT INTO error_catch_UploadtoSavix

        SELECT cast(ERROR_NUMBER() as nvarchar), ERROR_MESSAGE(),@PeriodID, @ProjectID, @UploadID,@DataID, getdate()
    END CATCH


    FETCH NEXT FROM projects1 INTO @ProjectID, @FAID
END


CLOSE projects1
DEALLOCATE projects1

SELECT 1 as success

这是我尝试做的并且陷入中间和不完整的事情:

select s.ID,u.* 
from T_PROJECTS_TEMP pt 
INNER JOIN
    (SELECT mgd.Gd_ID, mg.Grp_ID, mgd.Gd_CollectionDate, mgd.Gd_IsDeleted, mgd.Gd_CreateDate, mg.Grp_Project, mg.Grp_IsDeleted , mg.Grp_Legacy_ID,  p.LegacyProjectId
    FROM Savix_Service_Group..Group_Data mgd 
    INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_ID = mgd.Gd_CycleID
    INNER JOIN Savix_Service_Group..Groups mg ON mg.Grp_ID = mgc.Gc_GroupID 
    inner join SG_Dynamic_Forms..v_projects p ON p.ProjectID = mg.Grp_Project
    WHERE LegacyProjectId = 5047 AND Grp_IsDeleted = 0 AND Gd_IsDeleted != 1 )  dataset  on pt.ID = dataset.LegacyProjectId
INNER JOIN T_PERIODS s ON DATEDIFF(DAY,dataset.Gd_CollectionDate,dbo.fn_GetEndOfPeriod(s.ID)) >= 0 
LEFT JOIN T_UPLOADS u ON u.project_savix_ID = pt.ID AND u.PERIOD_ID = s.ID AND u.STATUS = 3 --AND u.UPLOADFILENAME != 'Automatic upload from web MIS'

WHERE NOT EXISTS (SELECT * FROM Savix_Service_Group..Group_Data mgd 
                    INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_ID = mgd.Gd_CycleID
                    WHERE mgc.Gc_GroupID = Grp_ID
                        AND DATEDIFF(dd,mgd.Gd_CollectionDate,(select dbo.fn_GetEndOfPeriod(s.ID))) >= 0 
                        AND (mgd.Gd_CollectionDate > Gd_CollectionDate) AND mgd.Gd_IsDeleted != 1 ) 
        AND NOT EXISTS (SELECT ID FROM T_UPLOADS u 
                    WHERE u.project_savix_ID = pt.ID AND u.PERIOD_ID = s.ID AND u.STATUS = 3 AND UPLOADFILENAME != 'Automatic upload from web MIS') 

ORDER BY s.ID

样本数据

T_Projects 和数据集的表和数据 - http://www.sqlfiddle.com/#!18/00205/2

T_Periods 的表格和数据 - http://www.sqlfiddle.com/#!18/3b6b1/2

T_uploads 的表和数据(在游标运行之前)- http://www.sqlfiddle.com/#!18/7d12f3/1

其他表数据 1 - http://www.sqlfiddle.com/#!18/6e499

其他表数据 2 - http://www.sqlfiddle.com/#!18/30e3b/3

函数 fn_getEndOFperiod - http://www.sqlfiddle.com/#!18/54d66

请注意 - 上述任何表格中都会有不止一行,但是我给出的示例只有一条记录。

例外数据

T_uploads 表 - http://sqlfiddle.com/#!18/027cc/1

T_ProjectsGroupData 总共将有 15 条记录,即对于每个 T_uploads.ID(与 T_ProjectsGroupData .UploadID 字段相关),T_ProjectsGroupData 中都会有一个条目

我除了将这个基于 CURSOR 的代码转换成一个简单的基于 SET 的方法之外,任何改进这个代码的帮助和建议都将是非常可观的。

4

1 回答 1

2
INSERT INTO T_PROJECTGROUPSDATA (uploadID, fieldA,fieldB,......fieldN )
SELECT t.ID,p.fieldA,mg.fieldB,......mgc.fieldN
FROM T_Projects mp
INNER JOIN T_UPLOADS t mp.ID = t.project_savix_ID
INNER JOIN  SG_Dynamic_Forms..v_projects p ON p.LegacyProjectId = mp.ID 
INNER JOIN Savix_Service_Group..Groups mg ON mg.Grp_Project = p.ProjectID 
INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_GroupID = mg.Grp_ID
INNER JOIN Savix_Service_Group..Group_Data mgd ON mgd.Gd_CycleID = mgc.Gc_ID
LEFT JOIN Savix_Service_Trainers..Trainers me ON me.Tr_ID = mgc.Gc_MonitoredBy
LEFT JOIN Savix_Service_Dictionaries..Dictionary mgt ON mgt.Dny_ID = me.Tr_Type 
WHERE mp.SyncMode = 1
AND t.[STATUS] = 3 AND t.UPLOADFILENAME = 'Automatic upload from web MIS'
AND mg.Grp_IsDeleted = 0 AND mgd.Gd_IsDeleted != 1


UPDATE t SET 
TOTALEXPENDITURES = CASE WHEN DATEDIFF(d,mp.PROJECTESTABLISHEDDATE,mp.EndDate) != 0 
    THEN p.Cost_Until_Today*DATEDIFF(d,mp.PROJECTESTABLISHEDDATE,dbo.fn_GetEndOfPeriod(t.Period_ID))/DATEDIFF(d,mp.PROJECTESTABLISHEDDATE,mp.EndDate)/dbo.fn_RateAtDate(p.PROJECTCURRENCY_ID,dbo.fn_GetEndOfPeriod(t.Period_ID))
    ELSE 0 
    END,
TotalExpendituresNative = CASE WHEN DATEDIFF(d,mp.PROJECTESTABLISHEDDATE,mp.EndDate) != 0 
    THEN p.Cost_Until_Today*DATEDIFF(d,mp.PROJECTESTABLISHEDDATE,dbo.fn_GetEndOfPeriod(t.Period_ID))/DATEDIFF(d,mp.PROJECTESTABLISHEDDATE,mp.EndDate)
    ELSE 0 
    END
FROM T_UPLOADS t
JOIN T_Projects mp ON mp.ID = t.project_savix_ID AND mp.SyncMode = 1
WHERE t.[STATUS] = 3 AND t.UPLOADFILENAME = 'Automatic upload from web MIS'

为了解释我是如何想到这个的,这里是下面代码的修改版本。我不能保证我得到了一切,我不得不做出一些假设。缺少样本数据,并且所有 savix 表都丢失了。鉴于信息并将我的时间限制在不到 4 小时的投入,这是我能想出的最佳答案。我可以做得更多,但你必须给我的不仅仅是网络名声。

--ALTER PROCEDURE [dbo].[proc_Upload]  WITH RECOMPILE 
--as
set NoCount on

DECLARE
    @StartTime      datetime,
    @EndTime        datetime,
    @DataID         uniqueidentifier,
    @CollectionDate datetime,
    @Status         int,
    @PeriodID       int,
    @EndDate        datetime,
    @GroupID        uniqueidentifier,
    @ProjectID      INT,
    @FAID           uniqueidentifier,
    @UploadID       int ,
    @Createdate datetime, 
    @MINIDprojects INT,
    @MAXIDprojects INT,
    @MINIDdatasets INT,
    @MAXIDdatasets INT,
    @MINIDperiods INT,
    @MAXIDperiods INT

很难工作和测试游标,因此我将它们(项目、项目1、数据集和周期)替换为包含标识的临时表并循环访问它们。

IF OBJECT_ID('tempdb..#projects') IS NOT NULL DROP TABLE #projects
IF OBJECT_ID('tempdb..#projects1') IS NOT NULL DROP TABLE #projects1
IF OBJECT_ID('tempdb..#datasets') IS NOT NULL DROP TABLE #datasets
IF OBJECT_ID('tempdb..#period') IS NOT NULL DROP TABLE #period

CREATE TABLE #projects
(
    [ProjectID] [INT],
    [Title] [varchar](255) ,
    [currency] [int] ,
    [Cost_Until_Today] [float] ,
    [StartDate] [datetime] ,
    [EndDate] [datetime] ,
    [MisID] [uniqueidentifier] ,
    [SystemStatus] [int] ,
    [FacilitatingAgency] [uniqueidentifier] ,
    [SyncMode] [int] 
)

CREATE TABLE #projects1
(
    ID INT IDENTITY(1,1),
    [ProjectID] [INT],
    [FacilitatingAgency] [uniqueidentifier]
)

CREATE TABLE #datasets
(
    ID INT IDENTITY(1,1),
    Gd_ID [uniqueidentifier], 
    Grp_ID [uniqueidentifier], 
    Gd_CollectionDate DATETIME, 
    Gd_IsDeleted BIT, 
    Gd_CreateDate DATETIME
)

CREATE TABLE #period
(
    ID INT IDENTITY(1,1),
    IDPeriod INT,
    EndDate DATETIME
)

 INSERT #projects ( [ProjectID], [Title], {currency], [Cost_Until_Today], [StartDate], [EndDate], [MisID], [SystemStatus], [FacilitatingAgency], [SyncMode] )
 SELECT ID, PROJECTNAME, PROJECTCURRENCY_ID, Cost_Until_Today, PROJECTESTABLISHEDDATE, EndDate, MisID, 4, FacilitatingAgency, SyncMode 
 FROM [dbo].[T_PROJECTS] /*thsi is the source table where every projectIDs need to be processed*/

Projects 包含来自 T_Projects 的所有 ID (projectID),此表中使用的唯一字段是 ID、projectcurrency、projectbuilteddate (startdate)、enddate 和 syncmode(必须为 = 1,我们稍后会看到)。我们可以忽略系统状态,因为它是一个常数。

表 dastasets 被删除并加载到这里。我们只关心 Savix_Service_Group..Group_Data 表中的 gd_id,但仍需要加入其他表,以防它们过滤掉我们不想要的值。插入记录时,此逻辑在下面再次使用。

IF exists ( select  * from    dbo.sysobjects where   id = object_id(N'[dbo].datasets]') and objectproperty(id, N'IsTable') = 1 ) DROP Table [dbo].datasets

SELECT mgd.Gd_ID, mg.Grp_ID, mgd.Gd_CollectionDate, mgd.Gd_IsDeleted, mgd.Gd_CreateDate, mg.Grp_Project, mg.Grp_IsDeleted , mg.Grp_Legacy_ID,  p.LegacyProjectId
INTO datasets
FROM Savix_Service_Group..Group_Data mgd 
        INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_ID = mgd.Gd_CycleID
        INNER JOIN Savix_Service_Group..Groups mg ON mg.Grp_ID = mgc.Gc_GroupID 
        inner join SG_Dynamic_Forms..v_projects p ON p.ProjectID = mg.Grp_Project

--DECLARE projects1 CURSOR LOCAL FOR Select distinct ProjectID, isnull([FacilitatingAgency], '00000000-0000-0000-0000-000000000000') from @projects P WHERE P.SystemStatus = 4 AND P.SyncMode = 1 
--/*First cursor - fetch the cursor from ProjectaTable*/

--OPEN projects1
--FETCH NEXT FROM projects1 INTO @ProjectID, @FAID
--WHILE @@FETCH_STATUS = 0

在这里,我们将项目 id 的不同值插入到 projects1 中。如果没有更多数据,我的假设是 T_projects 表中的 ID 是不同的,因此除了通过 SyncMode = 1 过滤之外,这一步在某种程度上是不必要的。请记住,SystemStatus 是一个常数,我们用值“4”填充它,这可以删除标准而没有任何影响。

INSERT INTO #projects1 ( ProjectID, FacilitatingAgency )
SELECT DISTINCT ProjectID, isnull([FacilitatingAgency], '00000000-0000-0000-0000-000000000000')  
FROM #projects p
WHERE SystemStatus = 4 AND SyncMode = 1

SELECT @MINIDprojects = MIN(ID), @MAXIDprojects = MAX(ID) FROM #projects1
WHILE @MINIDprojects < @MAXIDprojects + 1
BEGIN
--    BEGIN TRY
--        BEGIN TRAN

SELECT @ProjectID = ProjectID, @FAID = FacilitatingAgency FROM #projects1 WHERE ID = @MINIDprojects

        --DELETE FROM T_PROJECTGROUPSDATA WHERE T_PROJECTGROUPSDATA.UPLOAD_ID IN (SELECT ID FROM T_UPLOADS WHERE project_savix_ID = @ProjectID AND UPLOADFILENAME = 'Automatic upload from web MIS')

        --DECLARE datasets CURSOR LOCAL  FAST_FORWARD FOR SELECT Gd_ID, Grp_ID, Gd_CollectionDate, Gd_IsDeleted, Gd_CreateDate 
        --    FROM datasets
        --    WHERE LegacyProjectId = @ProjectID AND Grp_IsDeleted = 0 AND Gd_IsDeleted != 1 
        --    /*Second cursor - this will get the 'collectionDate'field from datasetsTable for every project fetched in above cursor and also get @dataID which is used to insert value in to other table-T_PROJECTGROUPSDATA*/
        --OPEN datasets

        --FETCH NEXT FROM datasets INTO @DataID, @GroupID, @CollectionDate, @Status, @Createdate
        --WHILE @@FETCH_STATUS = 0

在这里,我们通过 T_Project.ID AND Savix_Service_Group..Groups.Grp_IsDeleted = 0 AND Savix_Service_Group..Group_Data.Gd_IsDeleted != 1 插入#datasets 过滤

        INSERT INTO #datasets ( Gd_ID, Grp_ID, Gd_CollectionDate, Gd_IsDeleted, Gd_CreateDate )
        SELECT Gd_ID, Grp_ID, Gd_CollectionDate, Gd_IsDeleted, Gd_CreateDate FROM datasets WHERE LegacyProjectId = @ProjectID AND Grp_IsDeleted = 0 AND Gd_IsDeleted != 1 

        SELECT @MINIDdatasets = MIN(ID), @MAXIDdatasets = MAX(ID) FROM #datasets

        WHILE @MINIDdatasets < @MAXIDdatasets + 1
        BEGIN

            SELECT @DataID = Gd_ID, @GroupID = Grp_ID, @CollectionDate = Gd_CollectionDate, @Status = Gd_IsDeleted, @Createdate = Gd_CreateDate FROM #datasets WHERE ID = @MINIDdatasets

            --DECLARE period CURSOR LOCAL  FAST_FORWARD FOR SELECT ID, dbo.fn_GetEndOfPeriod(ID) FROM T_PERIODS
            --/* dbo.fn_GetEndOfPeriod(ID) - this function will give the end of the date of that specifc quarter for any given date*/
            --    WHERE DATEDIFF(dd,@CollectionDate,dbo.fn_GetEndOfPeriod(ID)) >= 0
            --    ORDER BY [YEAR],[Quarter]
            --    /*Third Cursor - this will process the records from another table called period with above fetched @collectionDate*/

            --OPEN period
            --FETCH NEXT FROM period INTO @PeriodID, @EndDate
            --WHILE @@FETCH_STATUS = 0

这可能是游标在这里最糟糕的用法。我们加载所有周期并循环浏览它们。最后,我们只关心 T_UPLOADS 表中的周期。

            INSERT INTO #period ( IDPeriod, EndDate ) SELECT ID, dbo.fn_GetEndOfPeriod(ID) FROM T_PERIODS WHERE DATEDIFF(dd,@CollectionDate,dbo.fn_GetEndOfPeriod(ID)) >= 0
            SELECT @MINIDperiods = MIN(ID), @MAXIDperiods = MAX(ID) FROM #period

            WHILE @MINIDperiods < @MAXIDperiods + 1
            BEGIN

                --IF EXISTS (SELECT * FROM Savix_Service_Group..Group_Data mgd 
                --    INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_ID = mgd.Gd_CycleID
                --    WHERE mgc.Gc_GroupID = @GroupID
                --        AND DATEDIFF(dd,mgd.Gd_CollectionDate,@EndDate) >= 0 
                --        AND (mgd.Gd_CollectionDate > @CollectionDate ) 
                --        AND mgd.Gd_IsDeleted != 1) 

                --BEGIN
                --    BREAK
                --END

在这里,我们跳过 UPLOADFILENAME != 'Automatic upload from web MIS' 的任何时间段

                --IF EXISTS (SELECT ID FROM T_UPLOADS u WHERE  u.project_savix_ID = @ProjectID AND u.PERIOD_ID = @PeriodID AND u.STATUS = 3 AND UPLOADFILENAME != 'Automatic upload from web MIS') 
                --BEGIN
                --    FETCH NEXT FROM period INTO @PeriodID, @EndDate
                --    CONTINUE
                --END

考虑到这一点,我们只关心 status = 3 和 UPLOADFILENAME = 'Automatic upload from web MIS'

                SET @UploadID = (SELECT ID FROM T_UPLOADS u WHERE  u.project_savix_ID = @ProjectID AND u.PERIOD_ID = @PeriodID AND u.[STATUS] = 3 AND UPLOADFILENAME = 'Automatic upload from web MIS')

                /*If T_uploads doesn't have appropirate period ID from cursor fetch then create a new entry in T_uploads with current projectID*/

                IF @UploadID IS NOT NULL
                        BEGIN

                            --declare @Project_ID_Legacy int = ISNULL((select distinct PROJECT_ID from T_UPLOADS where project_savix_ID = @ProjectID),@ProjectID)

为什么他们使用第二个表( T_UPLOADFIRSTSTEP )来生成他们只使用 id 的记录,然后在创建 T_Uploads 记录时使用该 ID 删除记录,这超出了我的理解,在我看来是糟糕的编码。

    --                            INSERT INTO T_UPLOADSFIRSTSTEP
    --                                   (PROJECT_ID
    --                                   --,UPLOADDATE
    --                                   --,UPLOADFILENAME
    --                                   --,UPLOADUSER_ID
    --                                   --,CURRENTSTEP
    --                                   ,[STATUS]
    --                                   ,Project_ID_MIS)
    --                             SELECT ISNULL((select distinct PROJECT_ID from T_UPLOADS where project_savix_ID = @ProjectID),@ProjectID),
    --                                    --GETDATE(),
    --                                    --'Automatic upload from web MIS',
    --                                    --2,
    --                                    --2,
    --                                    0,
    --                                    @ProjectID

插入 T_UPLOADS 总是会失败,因为有些字段不能为空,它们不在插入列表中并且没有分配给它们的默认值。

    --                            INSERT INTO T_UPLOADS ( ID, periodID, projectID,UPLOADDATE,UPLOADFILENAME,UPLOADUSER_ID )
    --                             SELECT uf.ID,
    --                                    @PeriodID,
    --                                    ISNULL((select distinct PROJECT_ID from T_UPLOADS where project_savix_ID = @ProjectID),@ProjectID),
    --                                    GETDATE(),
    --                                    'Automatic upload from web MIS',
    --                                    2
    --                             FROM T_UPLOADSFIRSTSTEP uf
    --                             INNER JOIN #projects mp ON  uf.Project_ID_MIS = mp.ProjectID
    --                             WHERE  uf.Project_ID_MIS = @ProjectID AND uf.[STATUS] = 0 
    --                                AND NOT EXISTS (SELECT * FROM T_UPLOADS u WHERE u.PROJECT_ID  = uf.PROJECT_ID AND u.PERIOD_ID = @PeriodID AND u.[STATUS] = 3)

    --                            DELETE FROM T_UPLOADSFIRSTSTEP WHERE STATUS = 0 AND PROJECT_ID = ISNULL((select distinct PROJECT_ID from T_UPLOADS where project_savix_ID = @ProjectID),@ProjectID)

    --                            --SET @UploadID = (SELECT ID FROM T_UPLOADS u WHERE  u.project_savix_ID = @ProjectID AND u.PERIOD_ID = @PeriodID AND u.[STATUS] = 3)
    --            END 
                --ELSE

所以这是在这 3 个游标内完成的真正逻辑。了解我们不关心周期,逻辑循环通过它们。我们只关心我们必须过滤某些条件(status = 3 和 UPLOADFILENAME = 'Automatic upload from web MIS'),并且他们有一个匹配的 T_Project.ID 表,条件为 T_Projects.SyncMode = 1

                            UPDATE t SET 
                                    TOTALEXPENDITURES = CASE WHEN DATEDIFF(d,mp.StartDate,mp.EndDate) != 0 
                                            THEN mp.Cost_Until_Today*DATEDIFF(d,mp.StartDate,dbo.fn_GetEndOfPeriod(t.Period_ID))/DATEDIFF(d,mp.StartDate,mp.EndDate)/dbo.fn_RateAtDate(mp.Currency,dbo.fn_GetEndOfPeriod(t.Period_ID))
                                            ELSE 0 
                                            END,
                                    TotalExpendituresNative = CASE WHEN DATEDIFF(d,mp.StartDate,mp.EndDate) != 0 
                                            THEN mp.Cost_Until_Today*DATEDIFF(d,mp.StartDate,dbo.fn_GetEndOfPeriod(t.Period_ID))/DATEDIFF(d,mp.StartDate,mp.EndDate)
                                            ELSE 0 
                                            END
                            FROM T_UPLOADS t
                            JOIN #projects mp ON mp.ProjectID = t.project_savix_ID
                            WHERE 1=1 -- t.ID = @UploadID 
                            --AND t.project_savix_ID = @ProjectID AND t.PERIOD_ID = @PeriodID 
                            AND t.[STATUS] = 3 AND t.UPLOADFILENAME = 'Automatic upload from web MIS'

插入 T_PROJECTGROUPDATA 复制了用于创建派生自 #datasets 的 @dataid 变量的逻辑。它源自我们在上面删除和创建的数据集表。

                        INSERT INTO T_PROJECTGROUPSDATA (uploadID, fieldA,fieldB,......fieldN )

                         SELECT @UploadID,p.fieldA,mg.fieldB,......mgc.fieldN
                         FROM #projects mp
                         inner join  SG_Dynamic_Forms..v_projects p ON p.LegacyProjectId = mp.projectID 
                         inner join Savix_Service_Group..Groups mg ON mg.Grp_Project = p.ProjectID 
                         INNER JOIN Savix_Service_Group..Group_Cycle mgc ON mgc.Gc_GroupID = mg.Grp_ID
                         INNER JOIN Savix_Service_Group..Group_Data mgd ON mgd.Gd_CycleID = mgc.Gc_ID
                         LEFT JOIN Savix_Service_Trainers..Trainers me ON me.Tr_ID = mgc.Gc_MonitoredBy
                         LEFT JOIN Savix_Service_Dictionaries..Dictionary mgt ON mgt.Dny_ID = me.Tr_Type 
                         --left join v1_Report_UDF_Data_UploadToSavix udf on udf.DataID = mgd.Gd_ID
                         WHERE mgd.Gd_ID = @DataID 

                --FETCH NEXT FROM period INTO @PeriodID, @EndDate
                SET @MINIDperiods = @MINIDperiods + 1
            END
            --CLOSE period
            --DEALLOCATE period

            --FETCH NEXT FROM datasets INTO @DataID, @GroupID, @CollectionDate, @Status, @Createdate
            SET @MINIDdatasets = @MINIDdatasets + 1
        END

        --CLOSE datasets
        --DEALLOCATE datasets

        --COMMIT
    --END TRY
    --BEGIN CATCH



        --SELECT ERROR_NUMBER(), ERROR_MESSAGE(),@PeriodID, @ProjectID, @UploadID,@DataID
        --IF CURSOR_STATUS('global' , 'period') >= 0
        --BEGIN
        --    CLOSE period
        --    DEALLOCATE uploadID
        --END

        --IF CURSOR_STATUS('global' , 'datasets') >= 0
        --BEGIN
        --    CLOSE datasets
        --    DEALLOCATE datasets
        --END

        --IF @@TRANCOUNT > 0
        --    ROLLBACK

        --INSERT INTO error_catch_UploadtoSavix

        --SELECT cast(ERROR_NUMBER() as nvarchar), ERROR_MESSAGE(),@PeriodID, @ProjectID, @UploadID,@DataID, getdate()
    --END CATCH

    SET @MINIDprojects = @MINIDprojects + 1
    --FETCH NEXT FROM projects1 INTO @ProjectID, @FAID
END


--CLOSE projects1
--DEALLOCATE projects1

--SELECT 1 as success

你有它。将近 300 行和 3 个光标压缩到 30 行且没有光标。

于 2019-06-28T00:14:28.147 回答