以块为单位轮询数据

时间:2012-02-01 18:16:07

标签: mysql sql sql-server tsql stored-procedures

我有如下的存储过程。

CREATE procedure [dbo].[Poll_EDI834_5010]
AS
BEGIN
select
    Header.ST01,
    Header.ST02,
    Header.ST03 ,
    Header.SE02,
    Line.INS01__InsuredIndicator ,
    Line.PER04__CommunicationNumber ,
      Line.PER05_CommunicationNumberQualifier ,
      Line.DMG06_CitizenshipStatusCode ,
         (select Member_Ref.REF01_ReferenceIdentificationQualifier ,Member_Ref.REF02_MemberSupplementalIdentifier
   from EDI834_5010_2000_RefLoop Member_Ref        
   where Member_Ref.Id_Header = Header.BGN02__TransactionSetIdentifierCode
   and Member_Ref.Id_Loop2000 = Line.Member_Id         
   for xml auto, elements, type) ,
    (select Member_DTP.DTP01_DateTimeQualifier , Member_DTP.DTP02_DateTimePeriodFormatQualifier , Member_DTP.DTP03_StatusInformationEffectiveDate
   from EDI834_5010_2000DTP Member_DTP        
   where Member_DTP.Header_Id = Header.BGN02__TransactionSetIdentifierCode
   and Member_DTP.Member_Id = Line.Member_Id         
   for xml auto, elements, type) ,
   VLine.HD01_MaintenanceTypeCode,
   VLine.HD03_InsuranceLineCode ,
    VLine.N402_ProviderStateCode ,
   VLine.N403_ProviderPostalZoneorZIPCode ,
      (select ClaimDTP.DTP01__DateTimeQualifier , ClaimDTP.DTP02__DateTimePeriodFormatQualifier , ClaimDTP.DTP03__StatusInformationEffectiveDate
     from EDI834_5010_2300_DTPLoop ClaimDTP
     where ClaimDTP.Id_Header = Header.BGN02__TransactionSetIdentifierCode
     and ClaimDTP.Id_Loop2000 = Line.Member_Id
     and ClaimDTP.Id_Loop2300 = VLine.Id_Claim
     for xml auto, elements, type)
from EDI834_5010_Header Header 
join EDI834_5010_2000 Line  on Header.BGN02__TransactionSetIdentifierCode = Line.Header_Id
join EDI834_5010_2300Loop VLine on Header.BGN02__TransactionSetIdentifierCode = VLine.Id_Header_Id andVLine.Id_Loop2000=Line.Member_Id
for xml auto,ELEMENTS
END
GO

它取得了10万条记录。我需要以块的形式获取数据。比如先10000然后下10000 ...我需要在存储过程中更改以获取块中的数据。

1 个答案:

答案 0 :(得分:0)

您的数据中需要某种索引,序列列或可订购ID列。我们称之为keyColumn。然后,您需要将最大潜在行数分成每次迭代所需的行数。我不知道你的架构,所以让我们像你在问题中所说的那样使用10000行。

然后你需要选择落入每次迭代的行数并执行...无论你想做什么。

SELECT @minID = Min(keyColumn) FROM table WHERE condition
SELECT @maxID = Max(keyColumn) FROM table WHERE condition
SELECT @potentialRows = 10000

WHILE @minID < @maxID
BEGIN
    SET @breakID = @minID + @potentialRows
    SELECT columns FROM table WITH (NOLOCK, ...)
    WHERE condition AND keyColumn BETWEEN @minID AND @breakID

    -- Do what you want with the selected rows here...

    SET @minID = @breakID + 1
END

如果您的keyColumn值很稀疏,则必须更改潜在的行计算。