Read csv file in SQL

17,036

As long as you can connect to the server, you will be able to create temp table.

For Microsoft SQL;

declare @TempTable csvtable 
     (
        firstCol varchar(50) NOT NULL,
        secondCol varchar(50) NOT NULL
     )

BULK INSERT @TempTable FROM 'PathToCSVFile' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')

GO

For MySQL;

CREATE TEMPORARY TABLE csvtable

LOAD DATA INFILE 'PathToCSVFile' 
INTO TABLE csvtable 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Share:
17,036
AlisonGrey
Author by

AlisonGrey

Trying to be better in college. :D

Updated on June 04, 2022

Comments

  • AlisonGrey
    AlisonGrey almost 2 years

    I have a csv file which I want to directly use without creating a table. Is there a way to read and manipulate it directly?