SQL 聯(lián)合查詢與XML解析實例詳解
SQL 聯(lián)合查詢與XML解析實例
這里舉例說明如何實現(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的時候,難點不在于SQL本身,而在于邏輯上,當寫出這個SQL以后,發(fā)現(xiàn)邏輯也沒有那么難了。
就是采用Union把兩組都查詢出來的表放到一個里面
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
sql中varchar和nvarchar的區(qū)別與使用方法
經(jīng)常用varchar總發(fā)現(xiàn)從access數(shù)據(jù)庫直接轉(zhuǎn)到mssql數(shù)據(jù)庫默認的都是nvarchar和ntext所以,找了一下,原來有這個說法。2008-01-01SQL Server誤區(qū)30日談 第23天 有關(guān)鎖升級的誤區(qū)
在SQL Server 2005和之前的版本,頁鎖會直接升級到表鎖。在SQL Server 2005或SQL Server 2008,你可以通過如下跟蹤標志改變鎖升級的行為2013-01-01Linux安裝ODBC連接SQLServer數(shù)據(jù)庫的步驟
這篇文章主要介紹了Linux安裝ODBC連接SQLServer數(shù)據(jù)庫?,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04SQL Server中實現(xiàn)二進制與字符類型之間的數(shù)據(jù)轉(zhuǎn)換
在SQL Server 數(shù)據(jù)庫中,如何實現(xiàn)二進制數(shù)據(jù)與字符串數(shù)據(jù)之間的直接轉(zhuǎn)換2012-11-11全國省市區(qū)縣最全最新數(shù)據(jù)表(數(shù)據(jù)來源谷歌)
因為工作項目需求,需要一個城市縣區(qū)數(shù)據(jù)表,上網(wǎng)搜了下,基本都不全,所以花了3天時間整理了一遍.2010-04-04