简体   繁体   English

如何从 SQL Server 存储过程执行 SSIS 包并传递参数

[英]How to execute SSIS package from SQL Server stored procedure and pass arguments

I'm trying to execute a SSIS package via a stored procedure.我正在尝试通过存储过程执行 SSIS 包。

I'm able to successfully execute that stored procedure only when the parameters are hard coded (like described in the first version of the code)只有当参数被硬编码(如代码的第一个版本中所述)时,我才能成功执行该存储过程

First version of the code:代码的第一个版本:

SELECT @Cmd = 'DTexec /F "C:\ssis\tool\package1.dtsx" 
                      /SET "\Package.Variables[User::ConfigurationName].Properties[Value]";"Report1" 
                      /SET "\Package.Variables[User::Country].Properties[Value]";"USA" 
                      /SET "\Package.Variables[User::OrgDepartment_Team].Properties[Value]";"USA" 
                      /SET "\Package.Variables[User::Subfix].Properties[Value]";"20190503" 
                      /SET "\Package.Variables[User::TeamName].Properties[Value]";"Team1"'

A record is created in database在数据库中创建一条记录

But the code below does not work (I'm passing the same parameter values)但是下面的代码不起作用(我传递了相同的参数值)

Second version of the code:代码的第二个版本:

SELECT @Cmd = 'DTexec /F "C:\ssis\tool\package1.dtsx" 
                      /SET "\Package.Variables[User::ConfigurationName].Properties[Value]";"' + @ConfigurationName + 
                   '" /SET "\Package.Variables[User::Country].Properties[Value]";"' + @Country + 
                   '" /SET "\Package.Variables[User::OrgDepartment_Team].Properties[Value]";"' + @OrgDepartment_Team +
                   '" /SET "\Package.Variables[User::Subfix].Properties[Value]";"' + @Subfix + 
                   '" /SET "\Package.Variables[User::TeamName].Properties[Value]";"' + @TeamName + '"'

There is no error but there is also no record created in the database.没有错误,但数据库中也没有创建记录。

DTExec: The package execution returned DTSER_SUCCESS (0). DTExec:包执行返回 DTSER_SUCCESS (0)。

I would appreciate any help :)我将不胜感激任何帮助 :)

Trying to figure out the issue试图找出问题

It looks like you are using a working syntax, but i will provide some suggestion that may helps to solve the issue:看起来您正在使用有效的语法,但我将提供一些可能有助于解决问题的建议:

(1) NULL handling (1) NULL 处理

Check that none of the variable used in the command contains NULL values which may cause that the concatenated string value will be NULL , you can use ISNULL() to solve the issue.检查命令中使用的变量是否不包含 NULL 值,这可能导致连接的字符串值为NULL ,您可以使用ISNULL()来解决问题。

SELECT @ConfigurationName = ISNULL(@ConfigurationName,'') , 
       @Country = ISNULL(@Country ,'') ,
       @OrgDepartment_Team = ISNULL(@OrgDepartment_Team,'') ,
       @Subfix  = ISNULL(@Subfix ,'') ,
       @TeamName = ISNULL(@TeamName,'')

(2) Quotes Handling (2) 行情处理

I prefer that quotes are included within the variables instead of the main string.我更喜欢将引号包含在变量中而不是主字符串中。 You can use QUOTENAME() function to achieve that:您可以使用QUOTENAME()函数来实现:

SELECT @ConfigurationName = QUOTENAME(ISNULL(@ConfigurationName,''),'"') , 
       @Country = QUOTENAME(ISNULL(@Country ,''),'"') ,
       @OrgDepartment_Team = QUOTENAME(ISNULL(@OrgDepartment_Team,''),'"') ,
       @Subfix  = QUOTENAME(ISNULL(@Subfix ,''),'"') ,
       @TeamName = QUOTENAME(ISNULL(@TeamName,''),'"')

SELECT @Cmd = 'DTexec /F "C:\ssis\tool\package1.dtsx" 
                      /SET "\Package.Variables[User::ConfigurationName].Properties[Value]";' + @ConfigurationName + 
                   ' /SET "\Package.Variables[User::Country].Properties[Value]";' + @Country + 
                   ' /SET "\Package.Variables[User::OrgDepartment_Team].Properties[Value]";' + @OrgDepartment_Team +
                   ' /SET "\Package.Variables[User::Subfix].Properties[Value]";' + @Subfix + 
                   ' /SET "\Package.Variables[User::TeamName].Properties[Value]";' + @TeamName 

(3) Reporting (3) 报告

You can read the entire package log by adding /Rep EWIP keyword to the command:您可以通过在命令中添加/Rep EWIP关键字来读取整个包日志:

SELECT @Cmd = 'DTexec /F "C:\ssis\tool\package1.dtsx" /Rep EWIP '

/Rep[orting] level [;event_guid_or_name[;event_guid_or_name[...]]: (Optional). /Rep[orting] 级别 [;event_guid_or_name[;event_guid_or_name[...]]:(可选)。 Specifies what types of messages to report.指定要报告的消息类型。 The available reporting options for level are as follows:可用的级别报告选项如下:

N No reporting. N 无报告。

E Errors are reported. E 报告错误。

W Warnings are reported. W 报告警告。

I Informational messages are reported. I 报告信息性消息。

C Custom events are reported. C 报告自定义事件。

D Data Flow task events are reported. D 报告数据流任务事件。

P Progress is reported. P 报告进度。

V Verbose reporting V 详细报告

References参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM