title: Making SQL query results 'PRINT'?
body: <p>I have a query which I eventually got to work fine but what I really need is the results to be displayed using the SQL <code>PRINT</code> command. The reason for this is I am automating the results to be emailed, and if I can have them come out as printed text then I can just embed the results in the email using the tool we use here. Otherwise, the current results have to be attached as a file and I would prefer the printed text if possible.</p>
  
  <p>I have tried to modify the query by adding <code>DECLARE</code> and <code>PRINT</code> but I am really confused and can't figure it out. The query has 2 CTE's in it pulling data from multiple databases. What it is doing is selecting all the sale numbers/ID's from our SAP system for yesterday and comparing them with our the Sale numbers/ID's from our POS system for yesterday to make sure every sale in our POS system is now in SAP. The query itself works fine.</p>
  
  <p>How can I print the results of this query?</p>
  
  <pre><code>WITH CTE1 (SAP_SALE)
  AS
  (          
    select distinct convert(BIGINT,convert(varchar(15),WERKS)+(select RIGHT(convert(Varchar(20),BONNR),7)))
    as Branch_tx_no from [PDP].[pdp].[S120] WITH (NOLOCK)
    where SPTAG &gt;= CAST(CONVERT(VARCHAR(10), GETDATE() -1, 101) AS DATETIME) AND
    SPTAG &lt; CAST(CONVERT(VARCHAR(10), GETDATE(), 101) AS DATETIME)
  ),
  CTE2 (AR_SALE)
  AS
  (
    select convert(varchar(15),branch_no)+convert(varchar(15),sale_tx_no)
    from [ARDB01].[PP_BODATA].[DBO].[sales_tx_hdr] WITH (NOLOCK)
    WHERE sale_date &gt;= CAST(CONVERT(VARCHAR(10), GETDATE() -1, 101) AS DATETIME) AND
    sale_date &lt; CAST(CONVERT(VARCHAR(10), GETDATE(), 101) AS DATETIME)
    and sale_type in ('C','L')
  )
  SELECT AR_SALE FROM CTE2 AS CTE2
    Left OUTER JOIN CTE1 AS CTE1
    ON CTE1.SAP_SALE = CTE2.AR_SALE
  WHERE CTE1.SAP_SALE IS NULL
  ORDER BY CTE2.AR_SALE
  </code></pre>