![](/img/trans.png)
[英]Define limits for varchar characters in SELECT statement SQL Server
[英]Select statement based on attributes in xml records as varchar in SQL Server
我有一个包含 2 列、 ID (Int)
和ResponseData (Varchar(MAX))
的表,我在其中存储了 XML 文本,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<soapTest:Response xmlns:soapTest="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:xs="http://www.w3.org/2001/XMLSchema" Version="2.0">
<soap:Issuer xmlns:soap="urn:oasis:names:tc:SAML:2.0:assertion">https://abc.sa/</soap:Issuer>
<soapTest:Status>
<soapTest:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
</soapTest:Status>
<soap:Assertion xmlns:soap="urn:oasis:names:tc:SAML:2.0:assertion" Version="2.0">
<soap:AttributeStatement>
<soap:Attribute Name="http://abc/xxx/englishFirstName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">JACK</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/englishName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Jack Mike</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/lang" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">en</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/dob" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Thu Jan 21 00:00:00 AST 1988</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/userid" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">1010026175</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/nationality" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Canadian</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/englishGrandFatherName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">test</soap:AttributeValue>
</soap:Attribute>
</soap:AttributeStatement>
</soap:Assertion>
</soapTest:Response>
现在我在表中有 100 多条记录,一些响应/xml 记录没有带有属性“Name= http://abc/xxx/englishName ”的标记/节点“属性”,我该如何返回所有这些记录没有这个节点?
例如
第一条记录具有以下标签/属性:
<soap:Attribute Name="http://abc/xxx/englishName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Jack Mike</soap:AttributeValue>
</soap:Attribute>
第二条记录没有它,响应没有属性 Name englishName...
我想用它的 ID 返回所有记录以知道哪个没有这个属性,结果是
ID responseData
---------------------------------------------------------------
12 <?xml version="1.0" encodxxxxxxxx
15 <?xml version="1.0" encodxxxxxxxx
87 <?xml version="1.0" encodxxxxxxxx
基于模糊的要求,请尝试以下操作。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY(1,1) PRIMARY KEY, ResponseData NVARCHAR(MAX));
INSERT INTO @tbl
VALUES (N'<soapTest:Response xmlns:soapTest="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:xs="http://www.w3.org/2001/XMLSchema" Version="2.0">
<soap:Issuer xmlns:soap="urn:oasis:names:tc:SAML:2.0:assertion">https://abc.sa/</soap:Issuer>
<soapTest:Status>
<soapTest:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</soapTest:Status>
<soap:Assertion xmlns:soap="urn:oasis:names:tc:SAML:2.0:assertion" Version="2.0">
<soap:AttributeStatement>
<soap:Attribute Name="http://abc/xxx/englishFirstName"
NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:string">JACK</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/englishName"
NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:string">Jack Mike</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/lang"
NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:string">en</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/dob"
NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:string">Thu Jan 21 00:00:00 AST 1988</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/userid"
NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:string">1010026175</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/nationality"
NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:string">Canadian</soap:AttributeValue>
</soap:Attribute>
<soap:Attribute Name="http://abc/xxx/englishGrandFatherName"
NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<soap:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:string">test</soap:AttributeValue>
</soap:Attribute>
</soap:AttributeStatement>
</soap:Assertion>
</soapTest:Response>');
-- DDL and sample data population, end
;WITH XMLNAMESPACES(DEFAULT 'urn:oasis:names:tc:SAML:2.0:assertion'
, 'urn:oasis:names:tc:SAML:2.0:protocol' AS [soapTest]), rs AS
(
SELECT *, TRY_CAST(ResponseData AS XML) AS [xml_data]
FROM @tbl
)
SELECT ID
, col.query('.') AS [xml_data]
FROM rs AS tbl
CROSS APPLY tbl.[xml_data].nodes('/soapTest:Response') AS tab(col)
WHERE col.exist('Assertion/AttributeStatement/Attribute/@Name[.="http://abc/xxx/englishName"]') = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.