我试图创建一个将创建外部数据源的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替换变量,则可以正常工作。即
CREATE EXTERNAL DATA SOURCE uploads
WITH
(
TYPE = BLOB_STORAGE,
LOCATION = 'https://somestorageaccount.blob.core.windows.net/uploads'
CREDENTIAL = azurecred
);
经过实验,我无法让它发挥作用。有可能吗?
答案 0 :(得分: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