Skip to content

Instantly share code, notes, and snippets.

@yorek
Created May 10, 2018 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorek/c16ac409e1281592812a714d584190ba to your computer and use it in GitHub Desktop.
Save yorek/c16ac409e1281592812a714d584190ba to your computer and use it in GitHub Desktop.
Bulk insert CSV data stored in a Blob Store
/*
Create the Database Master Key, if needed
*/
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'My-L0ng&Str0ng_P4ss0wrd!';
GO
/*
Create database scoped credentials to store the Shared Access Signature (SAS)
needed to access the Azure Blob Container. More info on Azure Blob SAS here:
https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1
SAS can be created right from Azure Portal or Azure CLI
*/
CREATE DATABASE SCOPED CREDENTIAL [Azure-Storage-Credentials]
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = '<your SAS signature, WITHOUT any leading "?">';
GO
/*
Now create the external data source and point it to the Azure Blob Store
*/
CREATE EXTERNAL DATA SOURCE [Azure-Storage]
WITH
(
TYPE = BLOB_STORAGE,
LOCATION = 'https://<storage-account>.blob.core.windows.net',
CREDENTIAL= [Azure-Storage-Credentials]
);
GO
/*
Bulk Import CSV into existing table
*/
BULK INSERT [<your-table>]
FROM '<container>/<file-path>/<file-name>'
WITH (DATA_SOURCE = 'Azure-Storage', FORMAT = 'CSV', FIRSTROW=2); /* For a CSV without column names don't specify the FIRSTROW option*/
go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment