我正在使用 sp_send_dbmail
获取 mailitem_id
作为输出。当我使用输出参数运行查询时
它抛出以下错误,并且没有输出,它工作正常。
Error: Execute SQL Task] Error: Executing the query "exec ? = sp_send_dbmail @profile_name=?, @recipien..." failed with the following error:
The batch could not be analyzed because of compile errors.
Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
查询:
exec ? = sp_send_dbmail @profile_name = ?, @recipients = ?,
@copy_recipients = ?, @subject = ?,
@body = ?, @importance = ?,
@body_format = ?@mailitem_id = ? OUT
@mailitem_id
是存储过程中的OUTPUT
参数,且为INT
类型。
我尝试了不同的方法来传递输出参数,但仍然遇到相同的错误...是否因数据类型不匹配或语法错误而失败,传递输出参数无法获取它..
请帮助我解决问题。我已在屏幕截图中添加了参数映射窗口。我也尝试过在 mailitem_id 的参数映射中使用
.Short
、ULarge_integer
和 Large_integer
类型而不是 Long
类型
最佳答案
可能是错误消息中描述的几件事之一,或全部。我将按照错误消息出现的顺序处理该错误消息。
解决错误消息
Problems with the query
- 使用
EXEC @return_status = ...
时语法中,T-SQL 变量必须使用@
定义象征。该变量也必须被声明,所以EXEC ?
仅当参数0
时才符合逻辑作为值传入,例如@return_value
. - 正如 marc_s 所评论的,该脚本在两个参数之间缺少逗号:
@body_format = ?@mailitem_id
. -
@mailitem_id = ? OUT
不是此输出参数的正确语法,除非?
的参数位置被定义为在脚本中声明的变量(类似于问题 #1)。
查询应该看起来像这样:
DECLARE @return_status INT, @mailitem_id INT;
EXEC @return_status = sp_send_dbmail @profile_name = ?, @recipients = ?,
@copy_recipients = ?, @subject = ?,
@body = ?, @importance = ?,
@body_format = ?, @mailitem_id = @mailitem_id OUTPUT
SELECT @return_status AS return_status, @mailitem_id AS mailitem_id;
有关执行此存储过程的一些示例,请参阅 sp_send_dbmail
documentation .
"ResultSet" property not set correctly
在执行 SQL 任务编辑器的常规选项卡中,ResultSet
的值是多少?在 Result Set
组?
选项是:
-
None
- 查询没有返回结果 -
Single row
- 查询仅返回一行 -
Full result set
- 查询返回多行 -
XML
- 查询返回XML格式的结果集
确保所选选项与脚本提供结果集的方式一致,并且任务配置的其余部分已配置为处理它。
parameters not set correctly
执行 SQL 任务编辑器左侧的结果集选项卡是将查询参数映射到 SSIS 变量的位置。确保您期望的映射变量根据 ResultSet
映射到此处。您在常规选项卡中选择。
把它们放在一起
使用我在“查询问题”下提供的代码,您可以设置 ResultSet
执行 SQL 任务编辑器的常规选项卡中的属性为 Single row
。然后,在结果集选项卡中,您将从脚本返回的列值( return_status
和 mailitem_id
)映射到 SSIS 变量(例如 User::Status
和 User::mailitem_id
,如您的屏幕截图)。
关于sql-server - 传递存储过程的输出参数时执行 SQL 任务时出现错误 "The batch could not be analyzed because of compile errors in ssis",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980582/