我有一個sql陳述句,用來發送郵件。作為一個例子,輸出結果如下。
select 'mail body content-1'/span> as 'mailBody'/span>, '11/9/221' as 'mailFooter', '[email protected]' as 'mail'.
union all
select 'mail body content-2' as 'mailBody'。 '11/09/2021' 作為 'mailFooter'。 '[email protected]' as 'mail'.
union all
select 'mail body content-3' as 'mailBody', '10/09/2021' 作為 'mailFooter'。 '[email protected]' as 'mail'.
在這里,我通過 "group by "為每個郵件發送郵件,并通過 "xml for path "來組合資料。
我的完整代碼如下
我的完整代碼如下
DECLARE @mail varchar(255)=''
DECLARE @mailBody nvarchar(max)='')
DECLARE db_cursor CURSOR FOR
select m.mail from (
--sql字串
選擇 'mail body content-1' 作為 'mailBody'。 '11/9/221' as 'mailFooter', '[email protected]' as 'mail'.
union all
select 'mail body content-2' as 'mailBody'。 '11/09/2021' 作為 'mailFooter'。 '[email protected]' as 'mail'.
union all
select 'mail body content-3' as 'mailBody', '10/09/2021' 作為 'mailFooter'。 '[email protected]' as 'mail'.
--sql String/
) m group by m.mail
---------------------------------------------------------------------------------------------------------------------------- Mail Cursor
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @mail
WHILE @@FETCH_STATUS=0
BEGIN
set @mailBody=(selectc. mailBody as 'span',c.mailFooter as 'small' from (
--sql字串
select 'mail body content-1' as 'mailBody', '11/9/221' 作為 'mailFooter'。 '[email protected]' as 'mail'.
union all
select 'mail body content-2' as 'mailBody'。 '11/09/2021' 作為 'mailFooter'。 '[email protected]' as 'mail'.
union all
select 'mail body content-3' as 'mailBody', '10/09/2021' 作為 'mailFooter'。 '[email protected]' as 'mail'.
--sql String/
) c where c.mail=@mail
for XML PATH('div')
)
select @mail,@mailBody
--EXEC msdb.dbo.sp_send_dbmail
FETCH NEXT FROM db_cursor INTO @mail
END @mail
CLOSE db_cursor
---------------------------------------------------------------------------------------------------------------------------- Mail Cursor/
DEALLOCATE db_cursor
結果它給了我以下輸出。
我的問題是,我必須寫2次相同的 "sql string "句子。
我的問題是,我必須把同一個 "sql字串 "寫兩次。
我怎樣才能擺脫這種重復出現的情況呢?
"sql字串 "是不斷變化的。但是在每個sql陳述句中都有一個叫做 "mail "的欄位。我打算把它保存為一個程序,并一直使用它。
我的路徑是否正確?
我的路徑是否正確?
我使用的是SQL Server 2016。
我不知道該如何處理String Agg、Conditional "why" loop、sp_execute、insert into @tempTable.
uj5u.com熱心網友回復:
你可以使用一個with塊來生成回圈的xml路徑:
DECLARE @mail varchar(255)=''
DECLARE @mailBody nvarchar(max)= ''。
DECLARE db_cursor CURSOR FOR
with tmp as (
--sql String as (
select 'mail body content-1' as 'mailBody', '11/9/221' 作為 'mailFooter'。 '[email protected]' as 'mail'.
union all
select 'mail body content-2' as 'mailBody'。 '11/09/2021' 作為 'mailFooter'。 '[email protected]' as 'mail'.
union all
select 'mail body content-3' as 'mailBody', '10/09/2021' 作為 'mailFooter'。 '[email protected]' as 'mail'.
--sql字串/
)
select m.mail, (select c.mailBody as 'span',c. mailFooter as 'small' from tmp c where c. mail=m.mail for XML PATH('div'))
from tmp m group by m.mail。
---------------------------------------------------------------------------------------------------------------------------- Mail Cursor
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @mail, @mailBody
WHILE @@FETCH_STATUS=0
BEGIN
select @mail,@mailBody
--EXEC msdb.dbo.sp_send_dbmail
FETCH NEXT FROM db_cursor INTO @mail, @mailBody
結束 結束CLOSE db_cursor
---------------------------------------------------------------------------------------------------------------------------- Mail Cursor/
DEALLOCATE db_cursor
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/311065.html
標籤:


