2015-03-03 72 views
-1

我有這個代碼到目前爲止,我想讓他們聯合到一個問題,但我不能得到它的工作。我一直試圖做一個新的選擇周圍轉換和一切下來作爲Nvarchar。我想做一個有兩個選擇的聯盟,並放在一個表

DECLARE @tableHTML NVARCHAR(MAX); 
SET @tableHTML = 
N'<H3>Felaktiga rader</H3>'+ 
N'<table border="1">'+ 
N'<tr><th>ID</th>'+ 
N'<th>Value</th>'+ 
N'<th>Reason</th>'+ 

CAST ((SELECT td=ID,'', 
td = Value,'', 
td = Reason,'' 

FROM Mail.dbo.CheckMe 
WHERE Value = '120' OR Value = '220' 
UNION 
Mail.dbo.CheckMeTwo 
WHERE Value = '120' OR Value = '220' 

for XML path ('tr'), TYPE 
) as NVARCHAR(MAX))+ 
N'</table>'; 

EXEC msdb.dbo.sp_send_dbmail 
@recipients = '[email protected];', 
@subject = ' Felaktiga Rader', 
@body = @tableHTML, 
@body_format = 'HTML'; 

回答

0

我這樣解決了。

FROM (SELECT * FROM Mail.dbo.CheckMe 
WHERE Value = '120' OR Value = '220' 
UNION 
SELECT * FROM Mail.dbo.CheckMeTwo 
WHERE Value = '120' OR Value = '220') T 
0

忘記了選擇在第二個查詢中的列!!!!

SELECT td=ID,'',td = Value,'', td = Reason,'' 
FROM Mail.dbo.CheckMe 
WHERE Value = '120' OR Value = '220' 
UNION 
SELECT td=ID,'',td = Value,'', td = Reason,'' 
FROM Mail.dbo.CheckMeTwo 
WHERE Value = '120' OR Value = '220' 

您需要在您的UNION結構中爲每個查詢選擇一個列表!

+0

沒有必要,在FROM查詢之前完成的那個 – Girre 2015-03-03 08:32:20

+0

當然有必要!你在回答中改變了它.... – CeOnSql 2015-03-03 08:46:33

相關問題