Public preview: Loading files from Azure Blob storage into Azure SQL Database

Thursday, February 23, 2017

Azure SQL Database enables you to directly load files stored in Azure Blob storage by using the following SQL statements:

  • BULK INSERT T-SQL command that loads a file from a Blob storage account into a SQL Database table
  • OPENROWSET table-value function that parses a file stored in Blob storage and returns the content of the file as a set of rows

The following example shows a BULK INSERT command that loads the content of the file into SQL Database:

BULK INSERT Product
FROM 'data/product.dat'
WITH ( DATA_SOURCE = 'MyAzureBlobStorageAccount');

You can parse the content of a remote file by using the OPENROWSET function and return rows from the file as results:

SELECT Name, Color, Price, Size, Quantity, Data, Tags
FROM OPENROWSET(BULK 'data/product.bcp', DATA_SOURCE = 'MyAzureBlobStorage',
                FORMATFILE='data/product.fmt', FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') as products;

As a prerequisite, you need to create an EXTERNAL DATA SOURCE that will point to your Azure Blob storage account. You'll use the name of this EXTERNAL DATA SOURCE in the DATA_SOURCE attribute. Here's an example of an EXTERNAL DATA SOURCE that points to a public Azure Blob storage account:

CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
WITH ( TYPE = BLOB_STORAGE, LOCATION = 'https://myazureblobstorage.blob.core.windows.net');

If your Azure Blob storage account is not public, you need to generate a shared access signatures (SAS) key for the account by using the Azure portal, put the SAS key in CREDENTIAL, and create an EXTERNAL DATA SOURCE with CREDENTIAL, as shown in the following example:

CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv=2015-12-11&ss=b&srt=sco&sp=rwac&se=2017-02-01T00:55:34Z&st=2016-12-29T16:55:34Z&spr=https&sig=copyFromAzurePortal';

CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
WITH ( TYPE = BLOB_STORAGE,
       LOCATION = 'https://myazureblobstorage.blob.core.windows.net',
       CREDENTIAL= MyAzureBlobStorageCredential);

 

For examples of code that loads the content of files from an Azure Blob Storage account, see SQL Server GitHub samples.

Free account

Learn and build with $200 in credit, and keep going for free

Start free

Visual Studio

Subscribers get up to $1800 per year of Azure services

Activate now

Startups

Join the BizSpark program and get free Azure services

Learn more