Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi coder,

I have to import data from excel (CSV) format into SQL server Database.
I know how to import csv file in db using bcp but only for one worksheet I want to import data of the second work in single query.

Create table test (customer_id int,telephone nvarchar(20))
DECLARE @CMD varchar(200)
SET @CMD='BCP db.dbo.test IN "Filepath" -S server details -T -f -t, -c -E'
EXEC Master..XP_CMDSHELL @CMD


Using the above code I am able to load the data from 1st worksheet of excel which is in CSV format but I also want to create second table and load the data of second worksheet into database in same query.

Any suggestions !!!

What I have tried:

<pre lang="text">Create table test (customer_id int,telephone nvarchar(20))
DECLARE @CMD varchar(200)
SET @CMD='BCP db.dbo.test IN "Filepath" -S server details -T -f -t, -c -E'
EXEC Master..XP_CMDSHELL @CMD
Posted
Updated 28-Jun-17 2:26am
v2

Csv files do not have multiple tabs. It is a single text file.

If you are saving the excel document as csv then it will only save the worksheet you are currently viewing.

Excel xlsx files are xml based
Older Excel files have a proprietary format.

You can create a data connection to excel, or import it using the import tools in MSSQL.

Simple way to import data into SQL Server[^]

Possible scripting method (I did not test it)
Importing an Excel Spreadsheet into a SQL Server Database - TechNet Articles - United States (English) - TechNet Wiki[^]
 
Share this answer
 
Quote:
I know how to import csv file in db using bcp but only for one worksheet I want to import data of the second work in single query.

CSV files don't have worksheets because csv files are flat data files.
First line is usually headers, and following lines are rows of data until the end.

Advice: if you want to manage worksheets in csv files, the easiest way is probably to have 1 csv file per worksheet.
 
Share this answer
 
Comments
amitesh1989 28-Jun-17 9:02am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900