SQL Server How to import CSV rows to SQL database columns

jdg4fx2g  于 2023-02-28  发布在  其他
关注(0)|答案(1)|浏览(152)

My source is a CSV file which has column names and its related data in rows. How to do manual mapping rows to columns in SQL. Below is the file structure.

CSV file
| EmpNo | ColumnNames | ColumnValues |
| ------------ | ------------ | ------------ |
| 1 | EmpName | 'John' |
| 1 | EmpDOB | '08/30/1985' |
| 1 | EmpDesignation | 'DBA' |

Table : EmployeeDetails

EmpNoEmpNameEmpDOBEmpDesignation
1John08/30/1985DBA
yqkkidmi

yqkkidmi1#

You can use openrowset to get data from csv file. Here an example that could help you.

INSERT INTO myTable (EmpNo, EmpName, EmpDesignation)
SELECT EmpNo, ColumnValues, ColumnNames
FROM OPENROWSET('BULK', 'C:\path\to\myFile.csv',
FORMATFILE = 'C:\path\to\myFormatFile.fmt') AS rows;

you have to improve columns declared in insert into table and select to matching csv columns with table columns.

相关问题