设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 891|回复: 0

Bulk Insert SQL Using Format File and Fixed Width Text File

[复制链接]
发表于 2011-10-6 13:46:58 | 显示全部楼层 |阅读模式
本帖最后由 Test 于 2011-10-6 16:24 编辑

Here is an example:
Let's say the fixed width text data file as:
  1. MONTHEND  BR   BU      INC DATE          TONS        COST
  2. 8/31/11  xdm  CBD-MS   8/31/11      555.2356     1584281
  3. 8/31/11  xdm  CBD-MS   9/ 1/11      275.7955      655156
  4. 8/31/11  xdm  CBD-MS  10/ 1/11      664.0000     1681941
复制代码

This file created under Linux and only has 0xD(\n) to make a new line.

and we create a format file like this:
  1. 9.0
  2. 6
  3. 1       SQLCHAR       0       8       "  "       1     MonthEnd            ""
  4. 2       SQLCHAR       0       3       ""         2     BR                  SQL_Latin1_General_CP1_CI_AS
  5. 3       SQLCHAR       0       10      ""         3     BU                  SQL_Latin1_General_CP1_CI_AS
  6. 4       SQLCHAR       0       8       ""         4     Inc_Date            ""
  7. 5       SQLCHAR       0       14      ""         5     TONS                ""
  8. 6       SQLCHAR       0       12      "\n"       6     COST                ""
复制代码

Please note the date data inside the source text file is as MM/DD/YY, it different from the DD/MM/YY we usually used.

So let have a test inside SQL server.

  1. Set DateFormat MDY

  2. if object_id('tempdb..#TxtData','U') is not null drop table #TxtData
  3. create table #TxtData
  4. (
  5.   --MonthEnd varchar(256)
  6.   MonthEnd datetime
  7.    ,BR varchar(3)
  8.    ,BU varchar(10)
  9.    ,Inc_Date datetime
  10. --,Inc_Date varchar(8)
  11.    ,TONS float
  12.    ,COST float
  13. )

  14. bulk insert #TxtData
  15. from 'C:\Source.txt'   -- The source text file.
  16. with (formatfile='C:\Source.fmt', FIRSTROW = 2); --the format file shows above

  17. select * from #TxtData

  18. /*
  19. Data format as
  20. MONTHEND  BR   BU      INC DATE          TONS        COST
  21. 8/31/11  xdm  CBD-MS   8/31/11      555.2356     1584281
  22. 8/31/11  xdm  CBD-MS   9/ 1/11      275.7955      655156
  23. 8/31/11  xdm  CBD-MS  10/ 1/11      664.0000     1681941


  24. Works Good with format file as:
  25. 9.0
  26. 6
  27. 1       SQLCHAR       0       8       "  "       1     MonthEnd            SQL_Latin1_General_CP1_CI_AS
  28. 2       SQLCHAR       0       3       ""         2     BR                  SQL_Latin1_General_CP1_CI_AS
  29. 3       SQLCHAR       0       10      ""         3     BU                  SQL_Latin1_General_CP1_CI_AS
  30. 4       SQLCHAR       0       8       ""         4     Inc_Date            SQL_Latin1_General_CP1_CI_AS
  31. 5       SQLCHAR       0       14      ""         5     TONS                ""
  32. 6       SQLCHAR       0       12      "\n"       6     COST                ""
  33. */
复制代码
That's it.
Data inside SQL DB shows up here:
  1. MonthEnd        BR        BU        Inc_Date        TONS        COST
  2. 2011-08-31 00:00:00.000        xdm          CBD-MS          2011-08-31 00:00:00.000        555.2356        1584281
  3. 2011-08-31 00:00:00.000        xdm          CBD-MS          2011-09-01 00:00:00.000        275.7955        655156
  4. 2011-08-31 00:00:00.000        xdm          CBD-MS          2011-10-01 00:00:00.000        664        1681941
复制代码

And, we can also use OPENROWSET to get text data directly. like:

  1. SELECT *
  2.       FROM  OPENROWSET(BULK  'C:\Source.txt',
  3.       FORMATFILE='C:\Source.fmt', FIRSTROW = 2) AS Test
复制代码

PS:
You might want to use XML format file, here I give out the example of text format file amd XML format with same data format for your reference.

Text format file:
  1. 9.0
  2. 11
  3. 1       SQLCHAR       0       12      ","      1     Fc_Year                      ""
  4. 2       SQLCHAR       0       12      ","      2     Fc_Month                     ""
  5. 3       SQLCHAR       0       12      ","      3     BU_idx                       ""
  6. 4       SQLCHAR       0       12      ","      4     Branch_idx                   ""
  7. 5       SQLCHAR       0       41      ","      5     Fc_Tons                      ""
  8. 6       SQLCHAR       0       41      ","      6     Fc_Cost                      ""
  9. 7       SQLCHAR       0       41      ","      7     Fc_Cost_ton                  ""
  10. 8       SQLCHAR       0       128     ","      8     Fc_create_id                 Latin1_General_CI_AS
  11. 9       SQLCHAR       0       24      ","      9     Fc_ctime                     ""
  12. 10      SQLCHAR       0       128     ","      10    Fc_update_id                 Latin1_General_CI_AS
  13. 11      SQLCHAR       0       24      "\r\n"   11    Fc_Uptime                    ""
复制代码

and XML format file as:
  1. <?xml version="1.0"?>
  2. <BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3. <RECORD>
  4.   <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="12"/>
  5.   <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="12"/>
  6.   <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="12"/>
  7.   <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="12"/>
  8.   <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="41"/>
  9.   <FIELD ID="6" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="41"/>
  10.   <FIELD ID="7" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="41"/>
  11.   <FIELD ID="8" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="128" COLLATION="Latin1_General_CI_AS"/>
  12.   <FIELD ID="9" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="24"/>
  13.   <FIELD ID="10" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="128" COLLATION="Latin1_General_CI_AS"/>
  14.   <FIELD ID="11" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="24"/>
  15. </RECORD>
  16. <ROW>
  17.   <COLUMN SOURCE="1" NAME="Fc_Year" xsi:type="SQLINT"/>
  18.   <COLUMN SOURCE="2" NAME="Fc_Month" xsi:type="SQLINT"/>
  19.   <COLUMN SOURCE="3" NAME="BU_idx" xsi:type="SQLINT"/>
  20.   <COLUMN SOURCE="4" NAME="Branch_idx" xsi:type="SQLINT"/>
  21.   <COLUMN SOURCE="5" NAME="Fc_Tons" xsi:type="SQLDECIMAL" PRECISION="19" SCALE="5"/>
  22.   <COLUMN SOURCE="6" NAME="Fc_Cost" xsi:type="SQLDECIMAL" PRECISION="19" SCALE="5"/>
  23.   <COLUMN SOURCE="7" NAME="Fc_Cost_ton" xsi:type="SQLDECIMAL" PRECISION="38" SCALE="19"/>
  24.   <COLUMN SOURCE="8" NAME="Fc_create_id" xsi:type="SQLNVARCHAR"/>
  25.   <COLUMN SOURCE="9" NAME="Fc_ctime" xsi:type="SQLDATETIME"/>
  26.   <COLUMN SOURCE="10" NAME="Fc_update_id" xsi:type="SQLNVARCHAR"/>
  27.   <COLUMN SOURCE="11" NAME="Fc_Uptime" xsi:type="SQLDATETIME"/>
  28. </ROW>
  29. </BCPFORMAT>
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2026-2-5 04:42 , Processed in 0.011278 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表