Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

CHUNK DATA INSERT

$
0
0

Hello All!

I'm inserting a table with millions of records, from another table.  I'm also parsing out some XML data.  My problem is this insert takes hours to do, when I leave for home at night, my session times out, and everything roll back to 0 rows inserted. I want to commit to 10000 at a time, so when it does time out, I can pick up where I left off.  I tried something below, but it doesn't save the records after I lose a connection.  Any Thoughts?

Thanks  Rudy

DECLARE @i INT = 1
WHILE @i <= 10000 BEGIN

IF @i % 10000 = 1 
       BEGIN TRANSACTION;

INSERT INTO CourtRecordEvent
                      (CaseNumber, CountyNumber, HistorySequenceNumber, EventType, EventDate, Tag, Timezone, [Description],
                       ctofcFirstName, ctofcLastName, ctofcMiddleName, ctofcSuffix, sealCtofcFirstName, sealCtofcLastName, sealCtofcMiddleName,
                        sealCtofcSuffix, courtRptrFirstName, courtRptrLastName, courtRptrMiddleName, courtRptrSuffix, DktText, IsMoneyEnabled, EventAmt, sealCtofcTypeCodeDescr)
 SELECT 
     COALESCE  (pref.value('(caseNo/text())[1]', 'varchar(20)'),'0') as CaseNumber,
     COALESCE (pref.value('(countyNo/text())[1]', 'int'),'0') as CountyNumber,
     COALESCE (pref.value('(histSeqNo/text())[1]', 'int'),'0') as HistorySequenceNumber,
     COALESCE (pref.value('(eventType/text())[1]', 'varchar(20)'),'0') as EventType,
     COALESCE (pref.value('(eventDate/text())[1]', 'datetime'),'0') as EventDate,
	COALESCE (pref.value('(tag/text())[1]', 'varchar(50)'),'0') as Tag,
      pref.value('(eventDate_TZ/text())[1]', 'varchar(20)') as Timezone,
      pref.value('(descr/text())[1]', 'nvarchar(max)') as [Description],
      pref.value('(ctofcNameF/text())[1]', 'nvarchar(150)') as ctofcFirstName,
      pref.value('(ctofcNameL/text())[1]', 'nvarchar(150)') as ctofcLastName,
      pref.value('(ctofcNameM/text())[1]', 'nvarchar(150)') as ctofcMiddleName,
      pref.value('(ctofcSuffix/text())[1]', 'nvarchar(20)') as ctofcSuffix,
      pref.value('(sealCtofcNameF/text())[1]', 'nvarchar(150)') as sealCtofcFirstName,
      pref.value('(sealCtofcNameL/text())[1]', 'nvarchar(150)') as sealCtofcLastName,
      pref.value('(sealCtofcNameM/text())[1]', 'nvarchar(150)') as sealCtofcMiddleName,
      pref.value('(sealCtofcSuffix/text())[1]', 'nvarchar(20)') as sealCtofcSuffix,
      pref.value('(courtRptrNameF/text())[1]', 'nvarchar(150)') as courtRptrFirstName,
      pref.value('(courtRptrNameL/text())[1]', 'nvarchar(150)') as courtRptrLastName,
      pref.value('(courtRptrNameM/text())[1]', 'nchar(10)') as courtRptrMiddleName,
      pref.value('(courtRptrSuffix/text())[1]', 'int') as courtRptrSuffix,
      pref.value('(dktTxt/text())[1]', 'nvarchar(max)') as DktText,
      pref.value('(isMoneyEnabled/text())[1]', 'bit') as IsMoneyEnabled,
      pref.value('(eventAmt/text())[1]', 'money') as EventAmt,
     pref.value('(sealCtofcTypeCodeDescr/text())[1]', 'nvarchar(50)') as sealCtofcTypeCodeDescr
        
FROM  
   dbo.CaseHistoryRawData_XML CROSS APPLY
      RawData.nodes('//CourtRecordEventCaseHist') AS CourtRec(pref)
      
     IF @i % 10000 = 0
      COMMIT;
 
  END
      
      
  
  GO
    
      
      
      


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>