sql SQL XML查询示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SQL XML查询示例相关的知识,希望对你有一定的参考价值。

WITH XMLNAMESPACES ('http://www.meridianlink.com/CLF' as ns)
SELECT XMLData.value('(/ns:VEHICLE_LOAN/ns:APPLICANTS/ns:APPLICANT/@is_declined)[1]','varchar(50)') as x,
XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') as LoanStatus,
XMLData.query('/ns:VEHICLE_LOAN/ns:LOAN_STATUS') as xy,
FileName,FileParent
from ml.MeridianLinkData
WHERE XMLData.value('(/ns:VEHICLE_LOAN/ns:APPLICANTS/ns:APPLICANT/@is_declined)[1]','varchar(50)') = 'N'
AND 
(XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') = 'APP' or
XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') = 'PRE-APP')


WITH XMLNAMESPACES ('http://www.meridianlink.com/CLF' as ns)
SELECT XMLData.value('(/ns:VEHICLE_LOAN/ns:APPLICANTS/ns:APPLICANT/@is_declined)[1]','varchar(50)') as x,
XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') as LoanStatus,
XMLData.query('/ns:VEHICLE_LOAN/ns:LOAN_STATUS') as xy,
FileName,FileParent
from ml.MeridianLinkData
WHERE XMLData.value('(/ns:VEHICLE_LOAN/ns:APPLICANTS/ns:APPLICANT/@is_declined)[1]','varchar(50)') = 'N'
AND 
(XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') = 'APP' or
XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') = 'PRE-APP')



WITH XMLNAMESPACES ('http://www.meridianlink.com/CLF' as ns)
SELECT XMLData.value('(/ns:VEHICLE_LOAN/ns:APPLICANTS/ns:APPLICANT/@is_declined)[1]','varchar(50)') as isDeclined,
XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') as LoanStatus,
XMLData.query('/ns:VEHICLE_LOAN/ns:LOAN_STATUS') as LoanStatusXML,
XMLData.value('(/ns:VEHICLE_LOAN/ns:SYSTEM/ns:BRANCH/@name)[1]','varchar(50)') as BranchName,
XMLData.value('(/ns:VEHICLE_LOAN/ns:SYSTEM/ns:BRANCH/@reference_id)[1]','varchar(50)') as BranchNumber,
FileName,FileParent
from ml.MeridianLinkData
WHERE XMLData.value('(/ns:VEHICLE_LOAN/ns:APPLICANTS/ns:APPLICANT/@is_declined)[1]','varchar(50)') = 'N'
AND 
(XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') = 'APP' or
XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') = 'PRE-APP')
AND XMLData.value('(/ns:VEHICLE_LOAN/ns:SYSTEM/ns:BRANCH/@name)[1]','varchar(50)') IS NOT NULL




WITH XMLNAMESPACES ('http://www.meridianlink.com/CLF' as ns)
SELECT x.BranchName, COUNT(x.AppCnt) AS TotalVL FROM (
SELECT XMLData.value('(/ns:VEHICLE_LOAN/ns:APPLICANTS/ns:APPLICANT/@is_declined)[1]','varchar(50)') as AppCnt,
XMLData.value('(/ns:VEHICLE_LOAN/ns:SYSTEM/ns:BRANCH/@name)[1]','varchar(50)') as BranchName
from ml.MeridianLinkData
WHERE XMLData.value('(/ns:VEHICLE_LOAN/ns:APPLICANTS/ns:APPLICANT/@is_declined)[1]','varchar(50)') = 'N'
AND 
(XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') = 'APP' or
XMLData.value('(/ns:VEHICLE_LOAN/ns:LOAN_STATUS/@loan_status)[1]','varchar(50)') = 'PRE-APP')
AND XMLData.value('(/ns:VEHICLE_LOAN/ns:SYSTEM/ns:BRANCH/@name)[1]','varchar(50)') IS NOT NULL) AS x
GROUP BY x.BranchName
WITH XMLNAMESPACES ('http://www.meridianlink.com/CLF' as AWMI)
SELECT XMLData.query('/AWMI:CREDITCARD_LOAN/AWMI:SYSTEM') as x, FileName,FileParent
from MeridianXML
where cast(XMLData.query('/AWMI:CREDITCARD_LOAN/AWMI:SYSTEM') as varchar(max)) != ''
INSERT INTO ml.MeridianLinkData(XmlData,RowHash,SSISDate,FileDate,FileParent,FileName)
VALUES(
(SELECT *
FROM OPENROWSET(
   BULK 'C:\Automation\MLDownload\Live\cc\CC_2098_OK.xml', SINGLE_BLOB) AS x),1231245877,'2015-1-1','2015-1-1','dfdfsd','sdfsdf')
GO
INSERT INTO MeridianXML(XmlData)
SELECT *
FROM OPENROWSET(
   BULK 'C:\Xml\cc\503_ok.xml', SINGLE_BLOB)
AS ImportSource
GO

SELECT XMLData.query('
     declare namespace AWMI="http://www.meridianlink.com/CLF";
        /AWMI:CREDITCARD_LOAN/AWMI:SYSTEM/@loan_number
    ') as x
from MeridianXML

create view vw_test
as
SELECT XMLData.value('
     declare namespace AWMI="http://www.meridianlink.com/CLF";
        (/AWMI:CREDITCARD_LOAN/AWMI:SYSTEM/@loan_number)[1]
    ','varchar(50)') as LoanNumber,
	XMLData.value('
     declare namespace AWMI="http://www.meridianlink.com/CLF";
        (/AWMI:CREDITCARD_LOAN/AWMI:LOAN_INFO/@is_complete_consumer)[1]
    ','varchar(50)') as LIComplete
from MeridianXML

以上是关于sql SQL XML查询示例的主要内容,如果未能解决你的问题,请参考以下文章

如何在sql中查询xml列

尝试将XML文件放入SQL Server表中

如何将 XML 文件(在本例中为业务流程事件日志)导入和查询到 SQL Server Express?

sql 用于XML示例的MS SQL

用DOM解析XML ,用xpath快速查询XML节点

可以查询 XML 模式集合的创建脚本吗?