在用于Azure Blob存储的Azure SQL数据库上创建外部表

dfddblmv  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(124)

是否可以为给定的Azure Blob存储创建外部表?我是否需要Synapse,如下所示:Creating External Table in Azure SQL DW from Azure BLOB Storage

0md85ypi

0md85ypi1#

在Azure SQL上创建外部表(也称为弹性查询)仅支持在Azure SQL数据库之间工作。
如果您希望从Azure存储帐户导入数据,则可以使用OPENROWSET或BULK INSERT,如下所示:

CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
WITH (  TYPE = BLOB_STORAGE,
        LOCATION = 'https://sqlchoice.blob.core.windows.net/sqlchoice/samples/load-from-azure-blob-storage',
--      CREDENTIAL= MyAzureBlobStorageCredential    --> CREDENTIAL is not required if a blob storage is public!
);

DROP TABLE IF EXISTS Product;
GO
--Create a permanent table.  A temp table currently is not supported for BULK INSERT,
--although it will will work with OPENROWSET
CREATE TABLE dbo.Product(
    Name nvarchar(50) NOT NULL,
    Color nvarchar(15) NULL,
    Price money NOT NULL,
    Size nvarchar(5) NULL,
    Quantity int NULL,
    Data nvarchar(4000) NULL,
    Tags nvarchar(4000) NULL
    --,INDEX cci CLUSTERED COLUMNSTORE
)
GO

BULK INSERT Product
FROM 'product.csv'
WITH (  DATA_SOURCE = 'MyAzureBlobStorage',
        FORMAT='CSV', CODEPAGE = 65001, --UTF-8 encoding
        FIRSTROW=2,
                ROWTERMINATOR = '0x0a',
        TABLOCK);

-- INSERT file exported using bcp.exe into Product table
BULK INSERT Product
FROM 'product.bcp'
WITH (  DATA_SOURCE = 'MyAzureBlobStorage',
        FORMATFILE='product.fmt',
        FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage',
        TABLOCK);

-- Read rows from product.dat file using format file and insert it into Product table
INSERT INTO Product WITH (TABLOCK) (Name, Color, Price, Size, Quantity, Data, Tags)
SELECT Name, Color, Price, Size, Quantity, Data, Tags
FROM OPENROWSET(BULK 'product.bcp',
                DATA_SOURCE = 'MyAzureBlobStorage',
                FORMATFILE='product.fmt',
                FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') as products;

-- Query remote file
SELECT Color, count(*)
FROM OPENROWSET(BULK 'product.bcp',
                DATA_SOURCE = 'MyAzureBlobStorage',
                FORMATFILE='data/product.fmt',
                FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') as data
GROUP BY Color;

相关问题