3

我正在尝试创建一个将创建外部数据源的 SQL 存储过程。该位置是传递给过程的参数(例如,天蓝色存储帐户容器)。我无法接受 Location 变量(如下面的代码片段所示)。

DECLARE @Location varchar(max)
SET @Location = 'https://somestorageaccount.blob.core.windows.net/uploads'

PRINT @Location
--DROP EXTERNAL DATA SOURCE uploads

CREATE EXTERNAL DATA SOURCE uploads
WITH
(
    TYPE = BLOB_STORAGE,
    LOCATION = @Location,
    CREDENTIAL = azurecred
);

错误消息是:

Msg 102, Level 15, State 1, Line 11
Incorrect syntax near '@Location'.

如果带有单引号的 url 替换了变量,它就可以工作。IE

CREATE EXTERNAL DATA SOURCE uploads
WITH
(
    TYPE = BLOB_STORAGE,
    LOCATION = 'https://somestorageaccount.blob.core.windows.net/uploads'
    CREDENTIAL = azurecred
);

经过试验,我无法让它发挥作用。可能吗?

4

1 回答 1

3

您不能在 DDL 语句中使用变量。尝试使用动态 sql - 为您的 DDL 形成字符串并使用 sp_executesql 存储过程执行它

ALTER PROCEDURE CETFromNewLocation    AS

BEGIN

DECLARE @location varchar(100)

SET @location = 'data/2015/2015831'

DECLARE @CreateExternalTableString varchar(100)

SET @CreateExternalTableString = 
                                    'Create External TABLE stg_tbl (
                                                  [DateId] int NULL
                                    )
                                    WITH (LOCATION = ' + @location + ',                                      
                                             DATA_SOURCE = my_external_source,
                                             FILE_FORMAT = my_external_file_format,
                                             REJECT_TYPE = VALUE,
                                             REJECT_VALUE = 0
                                    )'

EXEC sp_executesql @CreateExternalTableString

END
于 2018-02-07T19:48:29.923 回答