2

[来自客户的发布问题]

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

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'.
4

1 回答 1

6

您不能在 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
于 2015-09-01T01:58:45.897 回答