2

我有一个查询,我最终可以正常工作,但我真正需要的是使用 SQLPRINT命令显示的结果。这样做的原因是我正在自动化发送电子邮件的结果,如果我可以让它们以打印文本的形式出现,那么我可以使用我们在这里使用的工具将结果嵌入到电子邮件中。否则,必须将当前结果作为文件附加,如果可能,我更喜欢打印文本。

我试图通过添加来修改查询DECLAREPRINT但我真的很困惑,无法弄清楚。该查询有 2 个 CTE 从多个数据库中提取数据。它所做的是从我们的 SAP 系统中选择昨天的所有销售编号/ID,并将它们与我们昨天从我们的 POS 系统中的销售编号/ID 进行比较,以确保我们 POS 系统中的每笔销售现在都在 SAP 中。查询本身工作正常。

如何打印此查询的结果?

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
4

3 回答 3

3

最简单的解决方案是一次使用一个游标和PRINT一行。或者,如果结果中没有任何特殊字符,则可以使用 XML 连接:

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;
于 2013-01-07T22:44:24.133 回答
1

如果您需要使用 PRINT,您可以将查询结果组合成逗号(或其他字符)分隔的 VARCHAR 变量,然后打印该变量,例如

DECLARE @sTMP varchar(1000)
SET @sTMP = ''

-- Your CTE....
SELECT @sTMP = @sTMP + 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

PRINT @sTMP
于 2013-01-07T22:44:49.380 回答
0
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, row_number() over (order by AR_SALE) as r
into #temp  -- added this row right here 
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

然后...

declare @x varchar(100) 
declare @i int 
    set @i = 1 

while (@i <= (select max(r) from #temp)) begin
    select @x=AR_SALE from #temp where r=@i
    print @x
    set @i=@i+1
end 
于 2013-01-07T22:46:29.593 回答