Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql sp_send_dbmail计划作业仅在结果0时执行_Sql_Sql Server 2008_Scheduled Tasks_Sp Send Dbmail - Fatal编程技术网

Sql sp_send_dbmail计划作业仅在结果0时执行

Sql sp_send_dbmail计划作业仅在结果0时执行,sql,sql-server-2008,scheduled-tasks,sp-send-dbmail,Sql,Sql Server 2008,Scheduled Tasks,Sp Send Dbmail,我有以下预定的工作- Execute as login = 'sa' EXEC msdb.dbo.sp_send_dbmail @profile_name = 'SQL Mail Profile', @recipients = 'Tania.Mofflin@fuelfix.com.au', @subject = 'Wennsoft Service Management - Unposted Costs', @body = 'Unposted Costs from Closed POs:

我有以下预定的工作-

Execute as login = 'sa'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL Mail Profile',
@recipients = 'Tania.Mofflin@fuelfix.com.au',
@subject = 'Wennsoft Service Management - Unposted Costs',
@body = 'Unposted Costs from Closed POs:
        ',
@execute_query_database = 'FPLL',
@query = 'Select A.Service_Call_ID as "Service Call", A.Reference_TRX_Number as "Reference Number", A.WS_Committed_Cost as "Committed Cost"
            from SV000810 A LEFT JOIN POP10110 B on A.Reference_TRX_Number = B.PONUMBER and A.SEQNUMBR = B.ORD
            where A.WS_Committed_Cost <> 0 and B.QTYUNCMTBASE = 0'
这很好,但我想包括一个IF语句,它只在没有结果的情况下执行


谢谢你这是个老问题,但是。。。 由于您正在使用的作业步骤只是T-SQL,因此您可以执行以下操作:

IF exists(Select *
        from SV000810 A LEFT JOIN POP10110 B on A.Reference_TRX_Number = B.PONUMBER and 
        A.SEQNUMBR = B.ORD where A.WS_Committed_Cost <> 0 and B.QTYUNCMTBASE = 0)
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL Mail Profile',
@recipients = 'Tania.Mofflin@fuelfix.com.au',
@subject = 'Wennsoft Service Management - Unposted Costs',
@body = 'Unposted Costs from Closed POs:
        ',
@execute_query_database = 'FPLL',
@query = 'Select A.Service_Call_ID as "Service Call", A.Reference_TRX_Number as 
    "Reference Number", A.WS_Committed_Cost as "Committed Cost"
        from SV000810 A LEFT JOIN POP10110 B on A.Reference_TRX_Number = B.PONUMBER and 
        A.SEQNUMBR = B.ORD
        where A.WS_Committed_Cost <> 0 and B.QTYUNCMTBASE = 0'
END
请注意查询在这里是如何运行两次的,因此它的性能不是最好的。您可以将查询结果转储到临时表中,然后使用临时表执行EXISTS和email操作,或者如果愿意,也可以使用表变量。以上仅仅是实现您的目标的最快途径