[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to import a CSV file into an existing Azure SQL Server table

Posted on 2016-08-18
6
Medium Priority
?
584 Views
Last Modified: 2016-10-13
I have Visual Studio Community 2015.
I have an existing table with the correct columns, matching in order and data type the CSV file...
I was expecting something like a right click, import.   Seems like such a basic functionality.
What do you recommend?
0
Comment
Question by:Francois Koutchouk
  • 3
  • 2
5 Comments
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 2000 total points
ID: 41761507
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 2000 total points
ID: 41761510
Or if you can run a query against the database table you could use a process like below - just adjust table name/definition and name/path to actual CSV file. You can test this on a local SQL as well prior to running it against live cloud DB table:


--IMPORT
CREATE TABLE MySample
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO

--Create CSV file in drive C: with name Sample-Output.csv.txt with following content. The location of the file is C:\Sample-Output.csv.txt
1,James,Smith,19750101
2,Meggie,Smith,19790122
3,Robert,Smith,20071101
4,Alex,Smith,20040202


--Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
BULK
INSERT MySample
FROM 'c:\Sample-Output.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM MySample
GO
--From here on you can do anything you want with data imported via T-SQL Ccommands

--Drop the table to clean up database.
--DROP TABLE MySample
GO
0
 
LVL 2

Author Comment

by:Francois Koutchouk
ID: 41761649
Whoops except I get
Msg 40526, Level 16, State 1, Line 8
'BULK' rowset provider not supported in this version of SQL Server when connected to my Azure SQL Server.
0
 
LVL 2

Author Comment

by:Francois Koutchouk
ID: 41761731
In SQLyog front-end to MySQL, I just right-click on my RDS instance, select the CSV file and done...
0
 
LVL 40

Accepted Solution

by:
lcohan earned 2000 total points
ID: 41762915
Right...arghhh...well in that case BCP is the one that can save you and you should run a command like:

bcp database_name.schema.table in C:\import.csv -c -U username @servername -S servername.database.windows.net -P password -t

I added -t syntax to specify that it was comma delimited assuming that is your CSV delimiter and for more switches please see BCP command.

https://azure.microsoft.com/en-us/blog/bcp-and-sql-azure/
https://azure.microsoft.com/en-us/documentation/articles/sql-data-warehouse-load-with-bcp/
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As managed cloud service providers, we often get asked to intervene when cloud deployments go awry. Attracted by apparent ease-of-use, flexibility and low computing costs, companies quickly adopt leading public cloud platforms such as Amazon Web Ser…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question