SQL 聯(lián)合查詢與XML解析實(shí)例詳解
SQL 聯(lián)合查詢與XML解析實(shí)例
這里舉例說明如何實(shí)現(xiàn)該功能:
(select a.EBILLNO,
a.EMPNAME,
a.APPLYDATE,
b.HS_NAME,
replace(replace(a.SUMMARY,char(10), ''),char(13),'') as SUMMARY,
cast(c.XmlData as XML).value('(/List/item/No/text())[1]','NVARCHAR(300)') as No,
cast(c.XmlData as XML).value('(/List/item/zje/text())[1]','NVARCHAR(300)') as zje,
cast(c.XmlData as XML).value('(/List/item/yfje/text())[1]','NVARCHAR(300)') as yfje,
cast(c.XMLData as XML).value('(/List/item/bcje/text())[1]','NVARCHAR(300)') as bcje,
cast(c.XMLData as XML).value('(/List/item/URL/text())[1]','NVARCHAR(300)') as URL,
cast(c.XMLData as XML).value('(/List/item/Remark/text())[1]','NVARCHAR(300)') as BZ,
cast(p.XMLData as XML).value('(/NewDataSet/Table1/UserName/text())[1]','NVARCHAR(500)') as SKRXM,
('http://……?sid=3&mid=7281&PID='+a.PID) as bxdljdz
from Ex_Bill as a
left join Ex_System_Cfg as b on(a.BILLSYSTEMID=b.HS_ID and a.DATASYSTEMID=b.SYSTEM_NAME)
left join (select * from [10.2.3.39].AspireworkFlow.dbo.RepeaingTable) as c on (c.Keyword='URL' and c.ProcessID=a.PID)
left join (select * from [10.2.3.39].AspireworkFlow.dbo.RepeaingTable) as d on (d.Keyword='FKXX_New' and d.ProcessID=a.PID or d.Keyword='FKXX' and d.ProcessID=a.PID)
left join (select * from EX_BillExtension) as p on a.BILLNO=p.BILL_NO
where applyempid='zhongxun' and a.EBILLNO is not null
and status>5 and status not in(200,100,7000)
and a.APPLYDATE>'2011-01-01'
and a.HT='是'
and cast(d.XMLData as XML).value('(/List/item/SKRXM/text())[1]','NVARCHAR(300)') is null)
union
(select e.EBILLNO,
e.EMPNAME,
e.APPLYDATE,
f.HS_NAME,
replace(replace(e.SUMMARY,char(10), ''),char(13),'') as SUMMARY,
cast(g.XmlData as XML).value('(/List/item/No/text())[1]','NVARCHAR(300)') as No,
cast(g.XmlData as XML).value('(/List/item/zje/text())[1]','NVARCHAR(300)') as zje,
cast(g.XmlData as XML).value('(/List/item/yfje/text())[1]','NVARCHAR(300)') as yfje,
cast(g.XMLData as XML).value('(/List/item/bcje/text())[1]','NVARCHAR(300)') as bcje,
cast(g.XMLData as XML).value('(/List/item/URL/text())[1]','NVARCHAR(300)') as URL,
cast(g.XMLData as XML).value('(/List/item/Remark/text())[1]','NVARCHAR(300)') as BZ,
cast(h.XMLData as XML).value('(/List/item/SKRXM/text())[1]','NVARCHAR(300)') as SKRXM,
('http://……?sid=3&mid=7281&PID='+e.PID) as bxdljdz
from Ex_Bill as e
left join Ex_System_Cfg as f on(e.BILLSYSTEMID=f.HS_ID and e.DATASYSTEMID=f.SYSTEM_NAME)
left join (select * from [10.2.3.39].AspireworkFlow.dbo.RepeaingTable) as g on (g.Keyword='URL' and g.ProcessID=e.PID)
left join (select * from [10.2.3.39].AspireworkFlow.dbo.RepeaingTable) as h on (h.Keyword='FKXX_New' and h.ProcessID=e.PID or h.Keyword='FKXX' and h.ProcessID=e.PID)
where applyempid='zhongxun' and e.EBILLNO is not null
and status>5 and status not in(200,100,7000)
and e.APPLYDATE>'2011-01-01'
and e.HT='是'
and cast(h.XMLData as XML).value('(/List/item/SKRXM/text())[1]','NVARCHAR(300)') is not null)
在寫SQL的時(shí)候,難點(diǎn)不在于SQL本身,而在于邏輯上,當(dāng)寫出這個(gè)SQL以后,發(fā)現(xiàn)邏輯也沒有那么難了。
就是采用Union把兩組都查詢出來的表放到一個(gè)里面
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實(shí)現(xiàn)方法
這篇文章主要介紹了有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2015-10-10
sqlserver 存儲過程動態(tài)參數(shù)調(diào)用實(shí)現(xiàn)代碼
sqlserver 存儲過程動態(tài)參數(shù)調(diào)用實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10
sqlserver exists,not exists的用法
exists,not exists的使用方法示例,需要的朋友可以參考下。2009-12-12
數(shù)據(jù)庫中identity字段不必是系統(tǒng)產(chǎn)生的唯一值 性能優(yōu)化方法(新招)
具有identity特性的字段,其值是系統(tǒng)產(chǎn)生的,自動增加的,所以,一般把這個(gè)用在一個(gè)表的主鍵上。2011-09-09
SQLServer 數(shù)據(jù)庫備份過程中經(jīng)常遇到的九種情況
SQLServer 數(shù)據(jù)庫備份過程中經(jīng)常遇到各種問題,大家可以參照下面的問題,來分析下,快速的解決問題。2009-07-07
SQL語句練習(xí)實(shí)例之一——找出最近的兩次晉升日期與工資額
程序員們在編寫一個(gè)雇員報(bào)表,他們需要得到每個(gè)雇員當(dāng)前及歷史工資狀態(tài)的信息,以便生成報(bào)表。報(bào)表需要顯示每個(gè)人的晉升日期和工資數(shù)目。2011-10-10
實(shí)例講解sql server排名函數(shù)DENSE_RANK的用法
這篇文章主要以一個(gè)實(shí)例講解了sql server排名函數(shù)DENSE_RANK的用法,感興趣的小伙伴們可以參考一下2016-03-03
SQL Server 2016里的sys.dm_exec_input_buffer的問題
這篇文章主要介紹了SQL Server 2016里的sys.dm_exec_input_buffer的相關(guān)資料,需要的朋友可以參考下2016-04-04

