2013-01-07 142 views
2

我有一個查詢,我最終能夠正常工作,但我真正需要的是使用SQL PRINT命令顯示的結果。原因是我自動將結果通過電子郵件發送出去,如果我可以將它們作爲打印文本發佈出來,那麼我可以使用我們在此使用的工具將結果嵌入到電子郵件中。否則,當前結果必須作爲文件附加,如果可能的話,我寧願打印文本。使SQL查詢結果'PRINT'?

我試圖通過添加DECLAREPRINT修改查詢,但我真的很困惑,無法弄清楚。該查詢有2個CTE,用於從多個數據庫中提取數據。它所做的是在昨天從我們的SAP系統中選擇所有銷售編號/ ID,並將它們與我們昨天的POS系統中的銷售編號/編號進行比較,以確保我們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 
+1

假設這是SQL Server中,檢查出sp_send_dbmail http://msdn.microsoft.com/en-us/library/ms190307(v=sql .90).aspx – Laurence

+0

如果你在SQL Server @Laurence給出了一個很好的答案...使用電子郵件正文文本輸出任何你想要的。如果更改body_format參數,則可以使用HTML,也可以僅附加一些查詢結果。我認爲你必須在db中設置發送配置文件,但這並不難。 –

+0

嗨感謝您的快速回復。對不起,這是在SQL 2005上。我們使用名爲Opalis的腳本自動化工具來運行我們大部分的預定進程。我會每天早上運行這個查詢,並將結果作爲早晨檢查的一部分發送到我們的幫助臺。感謝那個建議勞倫斯我會調查,但理想情況下,所有我們的腳本等目前在Opalis我想把它放在那裏,但我會嘗試你的解決方案。謝謝。 – user1852208

回答

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; 
+0

非常感謝,完美的作品。 – user1852208

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 
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 [email protected] 
    print @x 
    set @[email protected]+1 
end