Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To configure bulk_options in OpenRowset to access a file located in Azure Blob Storage, follow these steps:

  1. Create a storage account and container in Azure Blob Storage. Upload the file that you want to access to this container.

  2. Obtain the SAS (Shared Access Signature) URL of the file. You can get this by right-clicking on the file in the Azure portal and selecting "Generate SAS" from the menu. Make sure to set the appropriate permissions based on your requirements.

  3. In SQL Server Management Studio (SSMS), create a linked server to the Azure Blob Storage account. You can use the sp_addlinkedserver stored procedure to create the linked server.

  4. Once the linked server is created, you can use the OPENROWSET function to access the file in the container. Here's an example:

SELECT * FROM OPENROWSET( BULK 'https://<your-storage-account>.blob.core.windows.net/<your-container>/<your-file>?<sas-token>', DATA_SOURCE = '<your-linked-server>', FORMAT = 'CSV', FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' ) AS data;

In this example, replace <your-storage-account>, <your-container>, <your-file>, <sas-token>, and <your-linked-server> with the appropriate values.

  1. You can also configure additional bulk_options based on your requirements, such as MAXERRORS, FIRSTROW, LASTROW, BATCHSIZE, etc. Here's an example:

SELECT * FROM OPENROWSET( BULK 'https://<your-storage-account>.blob.core.windows.net/<your-container>/<your-file>?<sas-token>', DATA_SOURCE = '<your-linked-server>', FORMAT = 'CSV', FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', MAXERRORS = 0, FIRSTROW = 2, LASTROW = 100, BATCHSIZE = 1000 ) AS data;

This example sets the MAXERRORS to 0 to fail the entire operation if there is any error, sets the FIRSTROW to 2 to skip the header row, sets the LASTROW to 100 to limit the number of rows returned, and sets the BATCHSIZE to 1000 to optimize performance.

Note that you need to have appropriate permissions to access the file in Azure Blob Storage and to create the linked server in SSMS. Make sure to follow security best practices and protect the SAS token to prevent unauthorized access.