简体   繁体   中英

Import data from TEXT file with one column into Table (many column) in SQL Server 2008

From a measuring instrument every 15 seconds getting data organized in this form

14.08.2014 15:00:00
UP:69.8,DN:70.7,Q=87
FLOW: 229.214  l/s
VEL: 1.18915 m/s

14.08.2014 15:00:15
UP:69.9,DN:70.5,Q=81
FLOW: 233.031  l/s
VEL: 1.20896 m/s

14.08.2014 15:00:30
UP:69.9,DN:70.7,Q=84
FLOW: 233.716  l/s
VEL: 1.21251 m/s

I need it import into SQL Server 2008, organized in the form shown in the table below, which I find very hard to do it, please give me an idea how to solve the problem. :

Data                   UP       DN      Q    FLOW        VEL
---------------------------------------------------------------
14.08.2014 15:00:00   69.8     70.7     87   229.214    1.18915
14.08.2014 15:00:15   69.9     70.5     81   233.031    1.20896
14.08.2014 15:00:30   69.9     70.7     84   233.716    1.21251

Please have a look at http://msdn.microsoft.com/en-us/library/ms188365.aspx

If you write the data to a file called data.txt you can use something like this:

    IF OBJECT_ID('tempdb..#rawdata') IS NOT NULL DROP TABLE #rawdata
    create table #rawdata (
     field1 varchar(255)
    , field2 varchar(255)
    , field3 varchar(255)
    , field4 varchar(255)
    )

    BULK INSERT #rawdata 
    FROM 'C:\data.txt'
    WITH (
        FIELDTERMINATOR = '\n'
      , ROWTERMINATOR = '\n\n'
      , TABLOCK
      )

    SELECT field1 As Data
    , SUBSTRING(field2, 4,4) AS UP
    , SUBSTRING(field2, 12,4) AS DN
    , SUBSTRING(field2, 19,2) AS Q
    , SUBSTRING(field3, 6,8) AS FLOW
    , SUBSTRING(field4, 5,8) AS VEL
    FROM #rawdata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM