使 SQL 查询结果“打印”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14205032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making SQL query results 'PRINT'?
提问by user1852208
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.
我有一个查询,我最终可以正常工作,但我真正需要的是使用 SQLPRINT
命令显示的结果。这样做的原因是我正在自动化将结果发送到电子邮件中,如果我可以让它们作为打印文本出现,那么我可以使用我们在这里使用的工具将结果嵌入到电子邮件中。否则,当前结果必须作为文件附加,如果可能,我更喜欢印刷文本。
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.
我试图通过添加DECLARE
和修改查询,PRINT
但我真的很困惑,无法弄清楚。查询中有 2 个 CTE,从多个数据库中提取数据。它所做的是从我们的 SAP 系统中选择昨天的所有销售编号/ID,并将它们与我们昨天的 POS 系统中的销售编号/ID 进行比较,以确保我们 POS 系统中的每个销售现在都在 SAP 中。查询本身工作正常。
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
回答by Sebastian Meine
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:
最简单的解决方案是一次使用一个游标和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;
回答by Yuriy Galanter
If you need to use PRINT you can combine results of your query into a comma (or other char) separated VARCHAR variable and then print that variable, e.g.
如果您需要使用 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
回答by gloomy.penguin
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
then...
然后...
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