import *.dat file into SQL database

  • Dear all,

    I have a test.dat file in attachment.

    I am finding how to import the "time,code,target.." in to SQL database.

    any one know how to do.

    Please help

    thank you.

  • The easiest way would be to use the import/export wizard in SSMS

  • DECLARE @xml XML

    SELECT @xml = BulkColumn

    FROM OPENROWSET

    (

    BULK 'D:\Test1.dat', SINGLE_BLOB

    ) X

    SELECT @xml

    SELECT X.C.value('text()[1]', 'nvarchar(50)') as Message,

    X.C.value('(/Msg/@time)[1]', 'datetime') as [Time],

    X.C.value('(/Msg/@type)[1]', 'nvarchar(50)') as [Type]

    FROM @xml.nodes('/Msg') X(C)

    -------------------------
    - Name?
    - Abu Dalah Sarafi.
    - Sex?
    - 3 times a week!
    - No, no. Male or Female?
    - Male, female, sometimes camel...

  • Every run well.

    Thank you very much.

  • Hello Artur Sokhikyan,

    Thank you again for you very well code, I still have a problem.

    that is :the code you gave me can only show the data for the first row.

    it looks like we point the array index to be only 1

    sample code: X.C.value('text()[1]','nvarchar(50)') as Message,

    So, do you have any how to show all existed records?

    Thank you very much.

  • try this.

    DECLARE @xml XML

    SELECT @xml = BulkColumn

    FROM OPENROWSET

    (

    BULK 'D:\Test1.dat', SINGLE_BLOB

    ) X

    SELECT

    C.value('.', 'nvarchar(50)') as [Message],

    C.value('@time', 'datetime') as [Time],

    C.value('@type', 'nvarchar(50)') as [Type],

    C.value('@code', 'nvarchar(50)') as [Code]

    FROM @xml.nodes('/Msg') X(C)

    -------------------------
    - Name?
    - Abu Dalah Sarafi.
    - Sex?
    - 3 times a week!
    - No, no. Male or Female?
    - Male, female, sometimes camel...

  • Yes,this is the perfect code.

    Thank you very much

    Yoothasak,

  • The query picked up few records. How to make it read whole file

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply