将 CSV 从 Blob 加载到 Azure SQL 服务器 - 未找到引用的外部数据源

Load CSV from Blob to Azure SQL Server - Referenced external data source not found

我正在尝试将 blob 存储中托管的 CSV 批量插入到 Azure SQL 服务器中,如 this MSDN post 中所述。

我的代码几乎全部取自this Microsoft Github sample

当 运行 时,我收到以下错误:

Referenced external data source "MyAzureBlobStorage" not found.

-- Create Database Master Key
IF (select Count(*) from sys.symmetric_keys where name like '%DatabaseMasterKey%') = 0
BEGIN
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MYPASSWORD'; 
END

-- Create Storage Credential
IF (select Count(*) from sys.database_credentials where name = 'MyAzureBlobStorageCredential') = 0
BEGIN
print 'Creating credential'
CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = '<secret>';
END

-- Create External Data Source
IF (select Count(*) from sys.external_data_sources where name = 'MyAzureBlobStorage') = 0
BEGIN
print 'creating external data source'
CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
WITH ( TYPE = BLOB_STORAGE,
    LOCATION = 'https://myaccount.blob.core.windows.net/upload',
    CREDENTIAL= MyAzureBlobStorageCredential);
END

-- Create temp table to hold data
IF OBJECT_ID('tempdb..#mytemptable') IS NOT NULL DROP TABLE #mytemptable
CREATE TABLE #mytemptable(
    [Id] [uniqueidentifier] NOT NULL
    -- etc
)

-- Bulk insert into temp table
BULK INSERT #mytemptable
FROM 'mycsv.csv'
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
        FORMAT='CSV', CODEPAGE = 65001, --UTF-8 encoding
        FIRSTROW=2,
        TABLOCK);

有什么想法吗?

这是另一个示例。请注意,秘密是从 Azure 门户复制的 "SAS token",没有前导 '?'

drop DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
drop  EXTERNAL DATA SOURCE MyAzureBlobStorage

go
-- Create Storage Credential
print 'Creating credential'
CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2017-05-08T23:03:46Z&st=2017-05-08T15:03:46Z&spr=https&sig=nxxxUJXfp%2BL23%2FULs2wY3%2BYAdFewzNsqp73rcsSoge4%3D';

-- Create External Data Source
--full url https://myaccount.blob.core.windows.net/files/tran.csv
print 'creating external data source'
CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
WITH ( TYPE = BLOB_STORAGE,
    LOCATION = 'https://myaccount.blob.core.windows.net/files',
    CREDENTIAL= MyAzureBlobStorageCredential);

--test
SELECT * FROM OPENROWSET(
   BULK  'tran.csv',
   DATA_SOURCE = 'MyAzureBlobStorage',
   SINGLE_CLOB) AS DataFile;

大卫

我遇到了同样的问题。我在@DavidBrowne-Microsoft 的评论中找到了答案,将其粘贴到此处以使其更加可见:

I repro'd, and BULK INSERT currently doesn't work against temp tables. Create a permanent table and try.