【问题标题】:Passing stored procedure to sp_send_dbmail将存储过程传递给 sp_send_dbmail
【发布时间】:2009-03-18 15:31:13
【问题描述】:

我在 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'

我也尝试过在 proc 上使用 4 部分命名;有和没有'EXEC'等。它作为一个查询工作得很好,但我似乎无法让它作为一个proc工作。这甚至可能吗?

【问题讨论】:

    标签: sql sql-server email stored-procedures


    【解决方案1】:

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

    @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 ;
    

    【讨论】:

      【解决方案2】:

      您是否尝试过创建用户定义函数而不是存储过程?

      类似:

      @query = 'select something from dbo.myFunction()'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 2012-10-13
        • 2016-04-27
        • 2016-04-05
        相关资源
        最近更新 更多