Dump data from .txt file into SQL Table using Bulk Insert.

  • Hello All,

    I have a requirement to dump the below data from .txt file into SQL Table using Bulk Insert. can someone please give me the command to parse the below data and pump it into the table?

    ID, Active, User, Client, Machine, StartTime, Duration, KernelTime, UserTime

    306287760, active, <unknown>, <unknown>, <unknown>, 12:07:13, 0:00:00.004, 0.000, 0.000

    306287758, netwait, <unknown>, <unknown>, <unknown>, 12:07:13, 0:00:00.041, 0.000, 0.000

    306287757, netwait, <unknown>, <unknown>, <unknown>, 12:07:13, 0:00:00.046, 0.000, 0.000

    306287755, netwait, <unknown>, <unknown>, <unknown>, 12:07:13, 0:00:00.050, 0.000, 0.000

    306287753, authwait, <unknown>, <unknown>, <unknown>, 12:07:13, 0:00:00.054, 0.000, 0.015

    306287752, netwait, <unknown>, <unknown>, <unknown>, 12:07:13, 0:00:00.085, 0.000, 0.000

    306287751, netwait, <unknown>, <unknown>, <unknown>, 12:07:13, 0:00:00.121, 0.000, 0.000

    306287746, authwait, <unknown>, <unknown>, <unknown>, 12:07:13, 0:00:00.126, 0.000, 0.000

  • Does it have to be bulk insert? An easy, quick way is using he import wizard in SSMS (right click on the database, Tasks\Import Data).

    Otherwise, here is the bulk insert msdn link, with examples:

    https://msdn.microsoft.com/en-us/library/ms188365(v=sql.110).aspx

    Thanks

    Michael

  • I would start by using bcp to create a format file, as described here: https://msdn.microsoft.com/en-us/library/ms191516.aspx

    Double check the format file, then use it as a parameter for the BULK INSERT command.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • figured it out.

    BULK INSERT SQLOPSDB.DBO.SDAdminStatus

    FROM 'S:\sdtemp\SDAdminsStatus_History.txt'

    WITH ( FIELDTERMINATOR=',', ROWTERMINATOR = '', KEEPNULLS, TABLOCK ,FIRSTROW = 2)

    GO

Viewing 4 posts - 1 through 3 (of 3 total)

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