Hey all! My first post on this forum here;I have this report i'm trying to set up so that any results that were already sent by e-mail in the report to be excluded the next time it runs.MY code looks like this at the moment and i'm using an additional column on my table where i can set a "flag" as per a suggestion i got on stackoverflow.if (select count(o.ord_hdrnumber)from orderheader owhere ord_billto in ('A','B','C','D')and DATEDIFF(hour , o.ord_startdate, GETDATE())<=24and ord_cmdvalue >= 250000and ord_status in ('avl','pln','std','cmp'))>0begin declare @tableHTML NVARCHAR(MAX); SET @tableHTML= N'<h1>Test Report</h1>'+ N'<table border=1>'+ N'<tr><th>Bill To</th>'+ N'<th>Reference number</th>'+ N'<th>Order #</th>'+ N'<th>Move #</th>'+ N'<th>Order Status</th>'+ N'<th>Value</th>'+ N'<th>Ship date</th>'+ N'<th>Delivery Date</th>'+ cast(( select td=o.ord_billto, '', td=o.ord_refnum, '', td=o.ord_hdrnumber, '', td=o.mov_number, '', td=o.ord_status, '', td=convert(decimal(10, 2), o.ord_cmdvalue), '', td=convert(varchar, o.ord_startdate, 101), '', td=convert(varchar, o.ord_completiondate, 101), '' from orderheader owhere ord_billto in ('A','B','C','D')and DATEDIFF(hour , o.ord_startdate, GETDATE())<=24and ord_cmdvalue >= 250000and ord_status in ('avl','pln','std','cmp') for XML path('tr'), type ) as NVARCHAR(MAX) ) + N'</table>' ; execute msdb.dbo.sp_send_dbmail @recipients='email@domain.com', @subject='Test Report', @body=@tableHTML, @body_format='HTML', @attach_query_result_as_file='false', @query_attachment_filename='EDI.csv', @query_result_separator='', @query_result_no_padding='FALSE'endupdate orderheader set ord_extrainfo2='1'where ord_billto in ('A','B','C','D')and DATEDIFF(hour , ord_startdate, GETDATE())<=24and ord_cmdvalue >= 250000and ord_status in ('avl','pln','std','cmp')and ord_extrainfo2='null'update orderheader set ord_extrainfo2='null'where ord_billto in ('A','B','C','D')and DATEDIFF(hour , ord_startdate, GETDATE())<=24and ord_cmdvalue >= 250000and ord_status in ('avl','pln','std','cmp')and ord_extrainfo2='1'
The part at the bottom with the update doesn't seem to be working properly as it's trying to update more rows than the query itself is pulling. Any suggestions of another way of doing this?1 of the reports we have already setup that does this has the update part of the script written like this, but i can't seem to see where my issue is:update orderheaderset ord_extrainfo1=1from orderheaderleft join carrier r on r.car_id=ord_carrierwhere ord_cmdvalue>r.car_ins_cargolimitsand DATEDIFF(day, ord_startdate, GETDATE())<30and ord_status in ('CMP','STD','PLN','AVL')and ord_carrier <> 'UNKNOWN'and ord_extrainfo1 is nullupdate orderheaderset ord_extrainfo1=NULLfrom orderheaderleft join carrier r on r.car_id=ord_carrierwhere (ord_cmdvalue<=r.car_ins_cargolimits or ord_carrier = 'UNKNOWN')and DATEDIFF(day, ord_startdate, GETDATE())<30and ord_status in ('CMP','STD','PLN','AVL')and ord_extrainfo1=1
Also, if someone can help me figure out how to get the DATDIFF function to pull the results properly; In the second example i gave with the UPDATE ORDERHEADER, they set it up to go back 30 days based on the order startdate, but when the report runs it only sends the most recent orders entered and anything before that is excluded...