Created
May 10, 2018 16:47
-
-
Save yorek/c16ac409e1281592812a714d584190ba to your computer and use it in GitHub Desktop.
Bulk insert CSV data stored in a Blob Store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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