Original address: http://bbs.51cto.com/thread-1133114-1.html
Problem Description:
create table [chr_sgs21] ( [Operation index number] [int] not null, [Call Reference Number] [bigint] not null, -- bulk insert [chr_sgs21] from ' E:\CHR_sgs21_20140528_111542-20140528_111819___ ( 20140521_090544-20140521_100056). Dat ' WITH (formatfile = ' D:\CDRFilterTool\ Filtertask/v3r7c01spc100_do\sgs21.fmt ') -- bulk insert [chr_ sgs21] from ' E:\CHR_sgs21_20140528_111819-20140528_111936___ (20140521_090209-20140521_ 100204). Dat ' WITH (formatfile = ' d:\cdrfiltertool\filtertask/v3r7c01spc100_do\ Sgs21.fmt ') -- bulk insert [chr_sgs21] from ' E:\CHR_sgs21_20140528_111936-20140528_112000___ (20140521_083339-20140521_100225). Dat ' WITH (formatfile = ' d:\cdrfiltertool\filtertask/v3r7c01spc100_do\sgs21.fmt ') -- bulk insert [chr_sgs21] from ' E:\CHR_sgs21_20140528_112000-20140528_ 112120___ (20140521_084601-20140521_100342). Dat ' WITH (formatfile = ' d:\ Cdrfiltertool\filtertask/v3r7c01spc100_do\sgs21.fmt ') -- bulk insert [CHR_sgs21] FROM ' E:\CHR_sgs21_20140528_112120-20140528_112236___ (20140521_ 080934-20140521_100452). Dat ' WITH (formatfile = ' d:\cdrfiltertool\filtertask/ V3r7c01spc100_do\sgs21.fmt ')      -- BULK INSERT  [CHR_SGS21] FROM ' E:\CHR_sgs21_20140528_112236-20140528_112354___ (20140521_084126-20140521_100600). Dat ' WITH (FORMatfile = ' d:\cdrfiltertool\filtertask/v3r7c01spc100_do\sgs21.fmt ') -- BULK INSERT [CHR_sgs21] FROM ' E:\CHR_sgs21_20140528_ 112354-20140528_112509___ (20140521_092117-20140521_100710). Dat ' WITH (formatfile = ' d:\cdrfiltertool\filtertask/v3r7c01spc100_do\sgs21.fmt ') -- bulk INSERT [CHR_sgs21] FROM ' E:\CHR_sgs21_20140528_112509-20140528_112624__ _ (20140521_085026-20140521_100821). Dat ' WITH (formatfile = ' D:\CDRFilterTool\ Filtertask/v3r7c01spc100_do\sgs21.fmt ') -- bulk insert [chr_ sgs21] from ' E:\CHR_sgs21_20140528_112624-20140528_112739___ (20140521_074949-20140521_ 100934). Dat ' WITH (formatfile = ' d:\cdrfiltertool\filtertask/v3r7c01spc100_do\ Sgs21.fmt ') -- BULK INSERT [CHR_sgs21] FROM ' E:\CHR_sgs21_20140528_ 112739-20140528_112854___ (20140521_073518-20140521_101044). Dat ' WITH (formatfile = ' d:\cdrfiltertool\filtertask/v3r7c01spc100_do\sgs21.fmt ') -- bulk INSERT [CHR_sgs21] FROM ' E:\CHR_sgs21_20140528_112854-20140528_113009__ _ (20140521_092749-20140521_101230). Dat ' WITH (formatfile = ' D:\CDRFilterTool\ Filtertask/v3r7c01spc100_do\sgs21.fmt ') -- bulk insert [chr_ sgs21] from ' E:\CHR_sgs21_20140528_113009-20140528_113124___ (20140521_083554-20140521_ 101425). Dat ' WITH (formatfile = ' d:\cdrfiltertool\filtertask/v3r7c01spc100_do\ Sgs21.fmt ')
From the above content can be seen, also more out of the "--", but also need to delete this, so that the above content automatically storage, at the same time,
BULK INSERT, the content of the future is still in constant increments, can not put the previous has been the storage of the re-execution! Ask for a script, thank you!
Solution:
1. The specific logic is to iterate through the DAT files in the Bulkinsert directory, and for each file, after performing the bulk insert operation, move the files to the archive directory.
2. After encapsulation into SP, the job timed execution.
3. The following script is not debugged. Please do the landlord's own commissioning. It is recommended to print the @sql\ @CMD variables before execution.
4. This issue can be used as an archiving scheme. Recommended reference for my blog:SQL Server Data Archiving Scenario
set nocount on;--Define Variables DECLARE @FullTableName nvarchar ( ) -- Full table name &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP, @BulkInsertFolder nvarchar (4000) --Data Catalog &NBSP, @ArchiveFolder nvarchar (4000) &NBSP;&NBSP;&NBSP, @FormatFile nvarchar ( ) --format file @cmd nvarchar (4000) ,@ Sql nvarchar (4000) ,@ Rtn int , @FileName nvarchar (4000); -- TEMP Variable: Data file name select @BulkInsertFolder = ' E:\BulkInsert\ ' &NBSP;&NBSP;&NBSP;&NBSP;&NBSP, @ArchiveFolder = ' E:\Archive\ ' &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP, @FullTableName = ' Chr_sgs21 ' &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP, @FormatFile = ' D:\CDRFilterTool\ Filtertask/v3r7c01spc100_do\sgs21.fmt ' ;-- receive CMD returns results CREATE TABLE #t (S nvarchar ( 4000)); -- Read file SELECT @cmd = ' dir /a:-d/b/o:n "' + @BulkInsertFolder + ' _*.dat '; insert #t (s) exec master. xp_cmdshell @cmd; declare @FileCount int; SELECT @FileCount = count (*) FROM #t where s is not null; while (@FileCount > 0 ) BEGIN SELECT TOP 1 @FileName = s FROM #t; DELETE #t WHERE s = @FileName; SELECT @FileCount = count (*) FROM #t where s is not null; & NBsp; select @sql = n ' BULK INSERT ' + @FullTableName + ' FROM ' + @BulkInsertFolder + @FileName + ' with ( formatfile = ' + @FormatFile + ' ) '; EXEC master.sys.sp_executesql @sql; -- Archive SELECT @cmd = ' move ' + @BulkInsertFolder + @FileName + ' ' + @ArchiveFolder; -- SELECT @cmd; exec @rtn = master. xp_cmdshell @cmd; if (@ @ERROR <> 0 OR @rtn <> 0) BEGIN CONTINUE; END END DROP TABLE #t;
This article is from the SQL Server deep dives blog, so be sure to keep this source http://ultrasql.blog.51cto.com/9591438/1584942
"51cto/bbs" help: How to accumulate data through stored procedures for automatic storage ...