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.

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

BULK INSERT Customers
    FROM 'C:\Data\Cust_Data.CSV'
    WITH
    (
    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.
1
 
brad2575Commented:
This can be done in SQL directly but SSIS is much better to import files.  A link to the basics is here:
https://docs.microsoft.com/en-us/sql/integration-services/lesson-1-create-a-project-and-basic-package-with-ssis
0
 
vsuripeddiAuthor Commented:
I accept this solution.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Recommendation to close this question by accepting the above comments as solution.
0
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.