SQL Server stored procedure to import Excel file into SQL Server table

I am using SQL Server 2012.
I need to import an Excel File and a CSV file into SQL Server tables.

I  Excel file is "C:\Data\Cust_Data.xlsx". The CSV is "C:\Data\Cust_Data.CSV".
The name of the SQL Server table where both CSV and Excel file should be uploaded is "Customers".
The database is "MainData". The CSV, Excel file and SQL Server table have the same fields.
Could some one please write a Stored procedure in SQL Server to import the Excel file into the SQL Server table.

Who is Participating?
Vitor MontalvãoMSSQL Senior EngineerCommented:
Here's almost everything you need to know:
USE MainData

    FROM 'C:\Data\Cust_Data.CSV'
    FIRSTROW = 2, -- remove this line if there is no leading row
    FIELDTERMINATOR = ',',  --field delimiter (depending on the region, some CSV uses ';')
    ROWTERMINATOR = '\n'   --row delimiter

Open in new window

Do the same for other files/tables. If you want to put it in a stored procedure, then it's very easy. Just learn about the CREATE PROCEDURE command.
This can be done in SQL directly but SSIS is much better to import files.  A link to the basics is here:
vsuripeddiAuthor Commented:
I accept this solution.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Recommendation to close this question by accepting the above comments as solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.