In SQL Server vNext 1.1 we now have the ability to import a csv via the BULK INSERT command without having to specify the field or row terminator
You still need to specify the format, if you only do something like the following
BULK INSERT AlexaSongs FROM 'c:\Songs played with Alexa.csv'
You will be greeted with these errors
Msg 4832, Level 16, State 1, Line 10
Bulk load: An unexpected end of file was encountered in the data file.
Msg 7399, Level 16, State 1, Line 10
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 10
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
So let's take a look at how this all works
First create the following table
USE tempdb GO CREATE TABLE AlexaSongs(PlayDate varchar(100), SongName varchar(200), Artist varchar(200), Album varchar(200)) GO
Now grab the csv file from here Songs played with Alexa.csv Either download the whole project and grab the file, or open in raw mode and copy and paste it into a file and save as Songs played with Alexa.csv
Now that you have the file and table ready, let's first take a look at how this was done before vNext 1.1
Here is what it looked like
BULK INSERT AlexaSongs FROM 'c:\Songs played with Alexa.csv' WITH ( FIELDTERMINATOR =',', ROWTERMINATOR = '\n' );
As you can see, we specified a comma as the field terminator and a newline as the row terminator
You could also get it to work by just specifying the field terminator in this case
BULK INSERT AlexaSongs FROM 'c:\Songs played with Alexa.csv' WITH ( FIELDTERMINATOR =',' );
So what does the new syntax look like?
Here is the code that accomplished the same as above but by using the new WITH FORMAT = CSV option
BULK INSERT AlexaSongs FROM 'c:\Songs played with Alexa.csv' WITH (FORMAT = 'CSV');
I guess you could say it is a little cleaner, but all this really is is syntactic sugar
For Azure, it looks like this, I grabbed this straight from this Books On Line Page here
First you need to create a data source
CREATE EXTERNAL DATA SOURCE MyAzureInvoices WITH ( TYPE = BLOB_STORAGE, LOCATION = 'https://newinvoices.blob.core.windows.net', CREDENTIAL = UploadInvoices );
And then you use that data source
BULK INSERT Colors2 FROM 'week3/inv-2017-01-19.csv' WITH (DATA_SOURCE = 'MyAzureInvoices', FORMAT = 'CSV');
For more examples including accessing data in a CSV file referencing a container in an Azure blob storage location go here https://msdn.microsoft.com/en-us/library/mt805207.aspx
That's all for today