每N行运行一次查询

时间:2014-11-03 21:57:38

标签: sql sql-server sql-server-2008 tsql

我有一个生产环境,我需要运行此代码并附加到表[RESULTS_TABLE],比方说,每个10K行FROM TABLE [SOURCETABLE]。我该怎么做呢? (由于系统和内存限制,我需要这样做)。

BEGIN
    SET NOCOUNT ON;

    IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[RESULTS_TABLE]') AND type IN (N'U'))
        DROP TABLE [RESULTS_TABLE]

    CREATE TABLE [RESULTS_TABLE](
    [CLAIM_ID] [nvarchar](15) NULL,
    [CIN] [nvarchar](10) NULL,
    [SVC_DATE] [datetime] NULL,
    [SVC_DATE_TO] [datetime] NULL,
    [TOTAL_DAYS] [int] NULL,
    [CHAIN_COUNT] [int] NULL
    ) ON [PRIMARY]

    ;WITH chain_builder AS
    (
    SELECT ROW_NUMBER() OVER(ORDER BY s.CIN, s.CLAIM_ID) as chain_ID,
      s.CIN,
      s.SVC_DATE, s.SVC_DATE_TO, s.CLAIM_ID, 1 as chain_count
    FROM [SOURCETABLE] s
    WHERE s.SVC_DATE <> ALL 
      (
      SELECT DATEADD(d, 1, s2.SVC_DATE_TO)
      FROM [SOURCETABLE] s2
      WHERE s.CIN = s2.CIN
      )
    UNION ALL
    SELECT chain_ID, s.CIN, s.SVC_DATE, s.SVC_DATE_TO,
      s.CLAIM_ID, chain_count + 1
      FROM [SOURCETABLE] s
    JOIN chain_builder as c
      ON s.CIN = c.CIN AND
      s.SVC_DATE = DATEADD(d, 1, c.SVC_DATE_TO)
    ),
    chains AS
    (
    SELECT chain_ID, CIN, SVC_DATE, SVC_DATE_TO,
      CLAIM_ID, chain_count, ROW_NUMBER() OVER(PARTITION BY chain_ID, chain_count ORDER BY SVC_DATE_TO DESC) as link_row
    FROM chain_builder
    ),
    link_picker AS
    (
    SELECT chain_ID, CIN, SVC_DATE, SVC_DATE_TO,
      CLAIM_ID, chain_count
    FROM chains
    WHERE link_row = 1
    ),
    diff AS
    (
    SELECT c.chain_ID, c.CIN, c.SVC_DATE, c.SVC_DATE_TO,
      c.CLAIM_ID, c.chain_count,
      datediff(day,c.SVC_DATE,c.SVC_DATE_TO)+1 daysdiff
    FROM link_picker c
    ),
    diff_sum AS
    (
    SELECT chain_ID, CIN, SVC_DATE, SVC_DATE_TO,
      CLAIM_ID, chain_count,
      SUM(daysdiff) OVER (PARTITION BY chain_ID) as total_diff
    FROM diff
    ),
    diff_comp AS
    (
    SELECT chain_ID, CIN,
      MAX(total_diff) OVER (PARTITION BY CIN) as total_diff
    FROM diff_sum
    )
    INSERT INTO [dbo].[LTAC_TEST_1]
    SELECT DISTINCT ds.CLAIM_ID, ds.CIN, ds.SVC_DATE,
      ds.SVC_DATE_TO, ds.total_diff as TOTAL_DAYS, ds.chain_count as CHAIN_COUNT
    FROM diff_sum ds
    JOIN diff_comp dc
    ON ds.chain_ID = dc.chain_ID AND ds.CIN = dc.CIN
      AND ds.total_diff = dc.total_diff
    OPTION (maxrecursion 0)
END
GO

2 个答案:

答案 0 :(得分:0)

您可以尝试创建存储过程。在程序中添加你的sql,但使用'top 10000'限制它的工作量。像这样:

SELECT TOP 10000 ROW_NUMBER() OVER(ORDER BY s.CIN, s.CLAIM_ID) as chain_ID, ...

然后设置SSIS作业以定期运行存储过程。如果有超过10,000个结果,它将在下一次运行存储过程时捕获下一个10,000。

答案 1 :(得分:0)

下一个10000

使用

选择前20000 ...

除了

选择前10000名