将存储过程传递给sp_send_dbmail

7

我正在使用SQL Server 2008中的sp_send_dbmail发送查询结果。我将查询移动到一个存储过程中,并尝试在sp_send_dbmail过程中使用该存储过程,如下所示:

EXEC msdb.dbo.sp_send_dbmail 
@profile_name               = 'myprofile',
@from_address               = 'email@somedomain.com',
@reply_to                   = 'email@somedomain.com',
@recipients                 = 'email@differentdomain.com',  
@importance                 = 'NORMAL', 
@sensitivity                = 'NORMAL', 
@subject                    = 'My Subject',
@body                       = 'Here you go.',
@attach_query_result_as_file= 1,
--@query_result_header      = 1,
@query_result_width         = 1000, 
@query_result_separator     = '~',
@query_attachment_filename  = 'myFile.txt',
@query                      = 'EXEC dbo.myProc'

我还尝试着在存储过程中使用四部分命名法,包括使用和不使用 'EXEC' 等方式。这样作为查询时没有问题,但我似乎无法将其作为存储过程执行。这是否可能?

2个回答

13

你需要添加数据库上下文:

@execute_query_database = 'MyDatabaseName',

我刚刚在AdventureWorks2008上运行了这个代码,没有遇到任何问题:

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'DBMail',
    @recipients = 'mitch@domain.com',
    @query = 'exec dbo.uspGetManagerEmployees 5' ,
    @execute_query_database = 'AdventureWorks2008',
    @subject = 'Work Order Count',
    @attach_query_result_as_file = 1 ;

0
你尝试过创建一个用户定义函数而不是存储过程吗?
类似这样: @query = 'select something from dbo.myFunction()'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接