Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSV into SQL (CODE)

Tags:

sql

sql-server

I want to import several CSV files automatically using SQL-code (i.e. without using the GUI). Normally, I know the dimensions of my CSV file. So, in many cases I create an empty table with, let say, x columns with the corresponding data types. Then, I import the CSV file into this table using BULK INSERT. However, in this case I don't know much about my files, i.e. information about data types and dimensions are not given.

To summerize the problem:

I receive a file path, e.g. C:...\DATA.csv. Then, I want to use this path in SQL-code to import the file to a table without knowing anything about it.

Any ideas on how to solve this problem?

like image 308
SWDQ Avatar asked Mar 09 '23 10:03

SWDQ


1 Answers

Use something like this:

BULK INSERT tbl
FROM 'csv_full_path'
WITH
(
FIRSTROW = 2, --Second row if header row in file
FIELDTERMINATOR = ',',  --CSV field delimiter
ROWTERMINATOR = '\n',   --Use to shift the control to next row
ERRORFILE = 'error_file_path',
TABLOCK
)

If columns are not known, you could try with:

select * from OpenRowset

Or, do a bulk insert with only the first row as one big column, then parse it to create the dynamic main insert. Or bulk insert the whole file into a table with just one column, then parse that...

like image 87
SAS Avatar answered Mar 19 '23 03:03

SAS