Bulk insert .txt file in SQL

后端 未结 1 1131
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 22:27

I\'m trying to import a .txt file into Advanced Query Tool (the SQL client I use). So far, I have:

CREATE TABLE #tb_test
(
id INTEGER,
name varchar(10),
dob date         


        
1条回答
  •  情歌与酒
    2021-01-22 22:41

    I was curious with this question and I found the following solution:

    Your data is comma separated but you are trying to split by TAB two options: change the file data to be TAB separated or change the fieldterminator = '\t' to fieldterminator = ','

    The DATE format has issues when loading directly from a file, my best solution is to change the temp field dob to type VARCHAR(20) and then, when passing to the final display/data storage convert to DATE.

    Here is the corrected code:

    CREATE TABLE #tb_test
    (
    id INTEGER,
    name varchar(10),
    dob varchar(20),
    city char(20),
    state char(20),
    zip integer
    );
    
    insert into #tb_test
    values
    (1,'TEST','2015-01-01','TEST','TEST',11111)
    ;
    
    bulk insert #tb_test
    from 'h:\tbdata.txt'
        with
        (
        fieldterminator = ',',
        rowterminator = '\n'
        );
    

    0 讨论(0)
提交回复
热议问题