Solved

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

Posted on 2016-08-18
6
22 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:FKoutchouk
  • 3
  • 2
6 Comments
 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 500 total points
ID: 41761507
0
 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 500 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 1

Author Comment

by:FKoutchouk
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 1

Author Comment

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

Accepted Solution

by:
lcohan earned 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now