Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 转换为XML的XQuery方法,exist()返回0_Sql Server_Xml_Sql Server 2008_Xquery Sql - Fatal编程技术网

Sql server 转换为XML的XQuery方法,exist()返回0

Sql server 转换为XML的XQuery方法,exist()返回0,sql-server,xml,sql-server-2008,xquery-sql,Sql Server,Xml,Sql Server 2008,Xquery Sql,查询sql server 2008时,我有一个表,其中一列将xmldata存储为文本,假设: TABLE(identifier varchar(15), xmldata text) xml看起来有点像这样,没有显式的根元素 <notRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns = "tons.o/NDA/

查询sql server 2008时,我有一个表,其中一列将xmldata存储为文本,假设:

TABLE(identifier varchar(15), xmldata text)
xml看起来有点像这样,没有显式的根元素

<notRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns = "tons.o/NDA/HIPAA/" attribute_1 = "value_1" ... attribute_n = "value_n" >
    <node nAttribute1 = "41pha0000" nAttribute2 = "VALUE" />
</notRoot>
是不是没有根元素? 是否必须将其存储在xml类型的变量中,才能在不同方法的MSDN引用之后使用此XQueryModel执行此操作? 有人能给我解释一下这种行为吗?

首先:

文本已经被弃用超过15年了。未来版本将不再支持它 如果必须处理XML,则应将其存储在XML类型的列中。真的,如果你能改变这一点,那就去做吧! 在提出有关SQL和XML的问题时,应始终附带一个现实的示例。像这样的事情对你来说可能很清楚,但对外部环境来说却不清楚。请阅读我在您的问题下方的评论中的问题,并提供更多详细信息! 我刚问了我的魔法水晶球,它告诉我,你可能在寻找这样的东西:

假设

一个条目中有许多元素 没有实际元素 所有节点中的属性名称都相同 没有名称空间 每人只有一个 试试这个:

DECLARE @dummy TABLE(identifier varchar(15), xmldata text);
INSERT INTO @dummy VALUES
('record 1','<notRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns = "tons.o/NDA/HIPAA/" 
              attribute_1 = "value_1" attribute_n = "value_n" >
                  <node nAttribute1 = "41pha0000" nAttribute2 = "VALUE" />
             </notRoot>
             <notRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns = "tons.o/NDA/HIPAA/" 
              attribute_1 = "other" attribute_n = "other_n" >
                  <node nAttribute1 = "xyz" nAttribute2 = "blah" />
             </notRoot>');


WITH XMLNAMESPACES(DEFAULT 'tons.o/NDA/HIPAA/') 
,Casted AS
(            
    SELECT t.identifier
          ,CAST(t.xmldata AS XML) AS TheXml
    FROM @dummy AS t
)
--the query

SELECT Casted.identifier
      ,nrAttr.value(N'local-name(.)',N'nvarchar(max)') AS notRoot_attr_name
      ,nrAttr.value(N'.',N'nvarchar(max)') AS notRoot_attr
      ,ndAttr.value(N'local-name(.)',N'nvarchar(max)') AS notRoot_attr_name
      ,ndAttr.value(N'.',N'nvarchar(max)') AS notRoot_attr
FROM Casted
OUTER APPLY Casted.TheXml.nodes(N'/notRoot/@*') AS A(nr)
OUTER APPLY nr.nodes(N'@*') AS B(nrAttr)
OUTER APPLY nr.nodes(N'node') AS C(nd)
OUTER APPLY nd.nodes(N'@*') AS D(ndAttr)
-一个声明的虚拟表来模拟您的场景

DECLARE @dummy TABLE(identifier varchar(15), xmldata text);
INSERT INTO @dummy VALUES
('record 1','<notRoot nr_a1 = "nr1_v1" nr_a2="nr1_v2">
                <node n1= "v1" n2="v2" n3="v3"/>
             </notRoot>
             <notRoot nr_a1 = "nr2_v1" nr_a2="nr2_v2">
                <node n1= "a" n2="b" n3="c"/>
             </notRoot>')
,('record 2','<notRoot nr_a1 = "one more" nr_a2="xyz">
                <node n1= "1" n2="2" n3="3"/>
             </notRoot>
             <notRoot nr_a1 = "and even more" nr_a2="more more more">
                <node n1= "100" n2="200" n3="300"/>
             </notRoot>');
WITH Casted AS
(            
    SELECT t.identifier
          ,CAST(t.xmldata AS XML) AS TheXml
    FROM @dummy AS t
)
结果是:

identifier  notRoot_a1  notRoot_a2       node_n1    node_n2 node_n3
record 1    nr1_v1          nr1_v2          v1      v2       v3
record 1    nr2_v1          nr2_v2          a       b        c
record 2    one more        xyz             1       2        3
record 2    and even more   more more more  100     200      300
使现代化 根据您的编辑,有一个默认名称空间,必须声明或通配符*:elementname

此外,您的编辑看起来好像事先不知道属性的数量或名称。因为中可能有n个属性,而中可能有n个属性,所以每个结果都会有一个属性。最好打两个不同的电话

如果您提前知道属性的名称,即使并非所有XML都使用它们,也会更容易

试试这个:

DECLARE @dummy TABLE(identifier varchar(15), xmldata text);
INSERT INTO @dummy VALUES
('record 1','<notRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns = "tons.o/NDA/HIPAA/" 
              attribute_1 = "value_1" attribute_n = "value_n" >
                  <node nAttribute1 = "41pha0000" nAttribute2 = "VALUE" />
             </notRoot>
             <notRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns = "tons.o/NDA/HIPAA/" 
              attribute_1 = "other" attribute_n = "other_n" >
                  <node nAttribute1 = "xyz" nAttribute2 = "blah" />
             </notRoot>');


WITH XMLNAMESPACES(DEFAULT 'tons.o/NDA/HIPAA/') 
,Casted AS
(            
    SELECT t.identifier
          ,CAST(t.xmldata AS XML) AS TheXml
    FROM @dummy AS t
)
--the query

SELECT Casted.identifier
      ,nrAttr.value(N'local-name(.)',N'nvarchar(max)') AS notRoot_attr_name
      ,nrAttr.value(N'.',N'nvarchar(max)') AS notRoot_attr
      ,ndAttr.value(N'local-name(.)',N'nvarchar(max)') AS notRoot_attr_name
      ,ndAttr.value(N'.',N'nvarchar(max)') AS notRoot_attr
FROM Casted
OUTER APPLY Casted.TheXml.nodes(N'/notRoot/@*') AS A(nr)
OUTER APPLY nr.nodes(N'@*') AS B(nrAttr)
OUTER APPLY nr.nodes(N'node') AS C(nd)
OUTER APPLY nd.nodes(N'@*') AS D(ndAttr)

好啊这里有几件事。首先,XML索引从1开始,而不是从0开始。因此,索引0处的元素不应该存在。您在第3/4次选择时会看到这一点。其次,我猜您的notRoot元素可能在另一个元素中,例如,顺便说一句,如果您知道XML中只有一个notRoot实例,那么您可以通过以下一种方式查询表:从SELECT identifier中选择a.b.query'.”,从SELECT identifier中选择xmldata=CASTxmldata作为XML FROM database.dbo.log l交叉应用l.xmldata.nodes'//notRoot'作为ab;你的方法有几个缺点。。。请提供一个包含真实数据的XML示例,以及您希望从中获得的输出。为什么理想情况下需要以xml形式访问该节点?这可能是一种误解,您只想说,您希望通过XML方法而不是字符串解析进行读取?重要问题:是否有一个或更多相同的元素?有很多属性吗?n_属性是否指向其中许多?属性总是一样的吗?-我已经提出了整个文本问题,就像我们的常驻向导一样。事实就是这样-现在数据更接近了,但我必须非常小心——给了你建议的大部分机会,但它仍然在值调用中返回null——但它让CTE工作,所以这已经是一个问题了help@RyanWood仍然返回null的已绑定到命名空间。。。默认名称空间是用xmlns=SomeName声明的。您可以不使用它,而是使用通配符*:元素。希望这有帮助…@RyanWood这个问题解决了吗?你需要进一步的帮助吗?你找到我上次的更新了吗?很抱歉耽搁了,SLA crunch。使用默认名称空间:XQuery[A.msg.nodes]:在类型“attribute*,xdt:untypedAtomic”中没有名为“@*”的属性。我也试着用这个名字。没有骰子。通配符返回null,xnmlnamespace N'blah'为x。。。valueN'/x:notRoot[1],N'varcharmax'->null。。。哦,伙计,这越来越令人沮丧了,不过我知道node属性的名称,在我们的例子中,只有2个,我们可以去掉notRoot属性,这样会更容易。在我看来,你已经是圣人了
DECLARE @dummy TABLE(identifier varchar(15), xmldata text);
INSERT INTO @dummy VALUES
('record 1','<notRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns = "tons.o/NDA/HIPAA/" 
              attribute_1 = "value_1" attribute_n = "value_n" >
                  <node nAttribute1 = "41pha0000" nAttribute2 = "VALUE" />
             </notRoot>
             <notRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns = "tons.o/NDA/HIPAA/" 
              attribute_1 = "other" attribute_n = "other_n" >
                  <node nAttribute1 = "xyz" nAttribute2 = "blah" />
             </notRoot>');


WITH XMLNAMESPACES(DEFAULT 'tons.o/NDA/HIPAA/') 
,Casted AS
(            
    SELECT t.identifier
          ,CAST(t.xmldata AS XML) AS TheXml
    FROM @dummy AS t
)
--the query

SELECT Casted.identifier
      ,nrAttr.value(N'local-name(.)',N'nvarchar(max)') AS notRoot_attr_name
      ,nrAttr.value(N'.',N'nvarchar(max)') AS notRoot_attr
      ,ndAttr.value(N'local-name(.)',N'nvarchar(max)') AS notRoot_attr_name
      ,ndAttr.value(N'.',N'nvarchar(max)') AS notRoot_attr
FROM Casted
OUTER APPLY Casted.TheXml.nodes(N'/notRoot/@*') AS A(nr)
OUTER APPLY nr.nodes(N'@*') AS B(nrAttr)
OUTER APPLY nr.nodes(N'node') AS C(nd)
OUTER APPLY nd.nodes(N'@*') AS D(ndAttr)
record 1    attribute_1 value_1 nAttribute1 41pha0000
record 1    attribute_1 value_1 nAttribute2 VALUE
record 1    attribute_n value_n nAttribute1 41pha0000
record 1    attribute_n value_n nAttribute2 VALUE
record 1    attribute_1 other   nAttribute1 xyz
record 1    attribute_1 other   nAttribute2 blah
record 1    attribute_n other_n nAttribute1 xyz
record 1    attribute_n other_n nAttribute2 blah