发送邮件查询结果从单独的表列添加内容

时间:2015-02-09 03:57:21

标签: sql sql-server sendmail

我的查询工作正常,但是除了客户编号之外,用户还需要客户的名称,并且发送到名称位于单独的客户表中,并且可以使用客户编号和客户编号的组合找到发货到号码。即,查看客户编号和发货编号的组合,以选择当前发货和物料代码之间的发货至名称字段。

 EXEC msdb.dbo.sp_send_dbmail
 @recipients=N'user@example.com',
 @body= 'Credits Over 20',
 @subject = 'Credits Over 20',
 @profile_name = 'SWFADmin',
 @execute_query_database='MMDC',
 @query_result_separator='',
 @query = 'Select Credits.TransactionNumber, Credits.RepNumber, Credits.CustomerID, Credits.ShipToId, Credits.ItemCode, Credits.Quantity, Credits.UnitMeasure, Credits.ReasonCode, Credits.ReceivedDate, Credits.TransmitStatus from (select TransactionNumber from Credits
 group by TransactionNumber
 having SUM (Quantity)>20) as creditsa

 Inner join Credits on Credits.TransactionNumber=creditsa.TransactionNumber

 where ReceivedDate > DateADD (day, -1, GetDate() )

 Order by transactionnumber, ReceivedDate',

 @attach_query_result_as_file=1

1 个答案:

答案 0 :(得分:0)

我在这里使用一些提示从头开始重新编写查询,这就是我最终的结果....感谢大家的帮助。

 Select Credits.TransactionNumber,Credits.RepNumber,Credits.CustomerID,Credits.ShipToId,Customers.SHIP_TO_NAME,Credits.ItemCode,Items.DESCRIPTION, Credits.Quantity,Credits.UnitMeasure,Credits.ReasonCode, Credits.ReceivedDate  from (select TransactionNumber from Credits
 group by TransactionNumber
 having SUM (Quantity)>20) as creditsa
 Inner join Credits on Credits.TransactionNumber=creditsa.TransactionNumber
 Inner join Customers on Credits.CustomerId=customers.CUSTOMER_ID and Credits.ShipToId=Customers.SHIP_TO_ID
 Inner join Device_Items on Credits.ItemCode=Items.ITEM_CODE
 where ReceivedDate > DateADD (day, -1, GetDate() )
 Order by RepNumber,transactionnumber,CustomerId, SHIP_TO_ID, ReceivedDate