Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9095323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:34:12+00:00 2026-06-16T23:34:12+00:00

I have a query which I eventually got to work fine but what I

  • 0

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 PRINT 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.

I have tried to modify the query by adding DECLARE and PRINT 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.

How can I print the results of this query?

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 >= CAST(CONVERT(VARCHAR(10), GETDATE() -1, 101) AS DATETIME) AND
  SPTAG < 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 >= CAST(CONVERT(VARCHAR(10), GETDATE() -1, 101) AS DATETIME) AND
  sale_date < 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:34:13+00:00Added an answer on June 16, 2026 at 11:34 pm

    The easiest solution is to use a cursor and PRINT one row at a time. Or you could use XML-concatenation, if you do not have any special characters in the result:

    DECLARE @txt NVARCHAR(MAX);
    
    
    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 >= CAST(CONVERT(VARCHAR(10), GETDATE() -1, 101) AS DATETIME) AND
      SPTAG < 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 >= CAST(CONVERT(VARCHAR(10), GETDATE() -1, 101) AS DATETIME) AND
      sale_date < CAST(CONVERT(VARCHAR(10), GETDATE(), 101) AS DATETIME)
      and sale_type in ('C','L')
    )
    SELECT @txt = (
    SELECT CHAR(13)+CHAR(10)+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
    FOR XML PATH(''),TYPE
    ).value('.','NVARCHAR(MAX)');
    
    PRINT @txt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom query which eventually returns a list of objects. I need
I have this query which on executing in my sql command line client executes
I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
I have a query which returns two rows of data as count. I want
I have this query which is a dependant query and taking much execution time
I have a query which looks like this: SELECT LossCost, CoverageID FROM BGILossCost] WHERE
I have a query which produces a list of orders like this: Invoice Description
I have this query which works correctly in MySQL. More background on it here
I have a query which searches two separate fields in the same table... looking
I have a query which returns a series of cells of data from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.