为 Polybase 创建存储过程 - 无法传入参数

Create a Stored Procedure for Polybase - Cannot pass in parameter

[客户发帖提问]

我想使用 Polybase 定期加载到 SQL 数据仓库。我的计划是创建一个存储过程,该过程接受用于创建外部 Table 的 "LOCATION" 参数,但该参数由于语法原因而失败。这是我编写的代码示例:

alter PROCEDURE CETFromNewLocation
as
begin
Declare @location varchar(100)='data/2015/2015831';
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
)
end 

我收到的错误消息是:

> Parse error at line: 25, column: 16: Incorrect syntax near
> '@location'.

您不能在 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