declare @xml xml,@sql varchar(2000)
  set @sql='root/SubMetaTable/'+@columnName+''
  set @xml=(select * from 表 where SUBMETA_PK=1069 for xml auto,ELEMENTS  XSINIL,xmlschema,root)对于这样生成的xml,带有xmlschema,如何读取节点的名称与文本,节点的数据类型
如: select B.v.value('local-name(.)','varchar(200)'),B.v.value('.',数据类型(从xml读取出来,因为生成的xmlschema会有数据类型)) from @xml.nodes('root/*')我这样读取,貌似取不到数据。。
  寻求合理的解决方案

解决方案 »

  1.   

    declare @xml xml,@sql varchar(2000)
     set @xml=(select * from 表 where SUBMETA_PK=1069 for xml auto,ELEMENTS XSINIL,xmlschema,root)
    -->读取节点名称与节点文本(节点为生成的字段)
      

  2.   

    /*
    sql xml 入门:
        --by jinjazz
        --http://blog.csdn.net/jinjazz
        
        1、xml:        能认识元素、属性和值
        
        2、xpath:    寻址语言,类似windows目录的查找(没用过dir命令的话就去面壁)
                    
                    语法格式,这些语法可以组合为条件:
                    "."表示自己,".."表示父亲,"/"表示儿子,"//"表示后代,
                    "name"表示按名字查找,"@name"表示按属性查找
                    
                    "集合[条件]" 表示根据条件取集合的子集,条件可以是
                        数  值:数字,last(),last()-数字 等
                        布尔值:position()<数字,@name='条件',name='条件'
                    条件是布尔值的时候可以合并计算:and or
        
        3、xquery:    基于xpath标的准查询语言,sqlserver xquery包含如下函数
                    exist(xpath条件):返回布尔值表示节点是否存在
                    query(xpath条件):返回由符合条件的节点组成的新的xml文档
                    value(xpath条件,数据类型):返回指定的标量值,xpath条件结果必须唯一
                    nodes(xpath条件): 返回由符合条件的节点组成的一行一列的结果表
    */declare @data xml
    set @data='
    <bookstore>
    <book category="COOKING">
      <title lang="en">Everyday Italian</title>
      <author>Giada De Laurentiis</author>
      <year>2005</year>
      <price>30.00</price>
    </book>
    <book category="CHILDREN">
      <title lang="jp">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>
    <book category="WEB">
      <title lang="en">XQuery Kick Start</title>
      <author>James McGovern</author>
      <author>Per Bothner</author>
      <author>Kurt Cagle</author>
      <author>James Linn</author>
      <author>Vaidyanathan Nagarajan</author>
      <year>2003</year>
      <price>49.99</price>
    </book>
    <book category="WEB">
      <title lang="cn">Learning XML</title>
      <author>Erik T. Ray</author>
      <year>2003</year>
      <price>39.95</price>
    </book>
    </bookstore>
    '--测试语句,如果不理解语法请参考上面的xpath规则和xquery函数说明--1、文档
    select @data
    --2、任意级别是否存在price节点
    select @data.exist('//price')
    --3、获取所有book节点
    select @data.query('//book')
    --4、获取所有包含lang属性的节点
    select @data.query('//*[@lang]') 
    --5、获取第一个book节点
    select @data.query('//book[1]')
    --6、获取前两个book节点
    select @data.query('//book[position()<=2]')
    --7、获取最后一个book节点
    select @data.query('//book[last()]')
    --8、获取price>35的所有book节点
    select @data.query('//book[price>35]')
    --9、获取category="WEB"的所有book节点
    select @data.query('//book[@category="WEB"]')
    --10、获取title的lang="en"的所有book节点
    select @data.query('//book/title[@lang="en"]')
    --11、获取title的lang="en"且 price>35的所有book节点
    select @data.query('//book[./title[@lang="en"] or price>35 ]')
    --12、获取title的lang="en"且 price>35的第一book的(第一个)title
    select @data.query('//book[./title[@lang="en"] and price>35 ]').value('(book/title)[1]','varchar(max)')
    --13、等价于12
    select @data.value('(//book[./title[@lang="en"] and price>35 ]/title)[1]','varchar(max)')
    --14、获取title的lang="en"且 price>35的第一book的(第一个)title的lang属性
    select @data.value('((//book[@category="WEB" and price>35 ]/title)[1]/@lang)[1]','varchar(max)')
    --15、获取第一本书的title
    select Tab.Col.value('(book/title)[1]','varchar(max)') as title
        from @data.nodes('bookstore')as Tab(Col) 
    --16、获取每本书的第一个author
    select Tab.Col.value('author[1]','varchar(max)') as title
        from @data.nodes('//book')as Tab(Col)
    --17、获取所有book的所有信息
    select
     T.C.value('title[1]','varchar(max)') as title,
     T.C.value('year[1]','int') as year,
     T.C.value('title[1]','varchar(max)')as title,
     T.C.value('price[1]','float') as price,
     T.C.value('author[1]','varchar(max)') as author1,
     T.C.value('author[2]','varchar(max)') as author2,
     T.C.value('author[3]','varchar(max)') as author3,
     T.C.value('author[4]','varchar(max)') as author4
    from @data.nodes('//book') as T(C)
    --18、获取不是日语(lang!="jp")且价格大于35的书的所有信息
    select
     T.C.value('title[1]','varchar(max)') as title,
     T.C.value('year[1]','int') as year,
     T.C.value('title[1]','varchar(max)')as title,
     T.C.value('price[1]','float') as price,
     T.C.value('author[1]','varchar(max)') as author1,
     T.C.value('author[2]','varchar(max)') as author2,
     T.C.value('author[3]','varchar(max)') as author3,
     T.C.value('author[4]','varchar(max)') as author4
    from @data.nodes('//book[./title[@lang!="jp"] and price>35 ]') as T(C)
      

  3.   

    declare @xml xml
    set @xml='
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified">
        <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
        <xsd:element name="ACCNOSET">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Id" type="sqltypes:int" nillable="1" />
              <xsd:element name="Cate_Code" nillable="1">
                <xsd:simpleType>
                  <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1028" sqltypes:sqlCompareOptions="IgnoreKanaType IgnoreWidth">
                    <xsd:maxLength value="10" />
                  </xsd:restriction>
                </xsd:simpleType>
              </xsd:element>
              <xsd:element name="GP" nillable="1">
                <xsd:simpleType>
                  <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1028" sqltypes:sqlCompareOptions="IgnoreKanaType IgnoreWidth">
                    <xsd:maxLength value="10" />
                  </xsd:restriction>
                </xsd:simpleType>
              </xsd:element>
              <xsd:element name="COMPANY" nillable="1">
                <xsd:simpleType>
                  <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1028" sqltypes:sqlCompareOptions="IgnoreKanaType IgnoreWidth">
                    <xsd:maxLength value="10" />
                  </xsd:restriction>
                </xsd:simpleType>
              </xsd:element>
              <xsd:element name="DEPT" nillable="1">
                <xsd:simpleType>
                  <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1028" sqltypes:sqlCompareOptions="IgnoreKanaType IgnoreWidth">
                    <xsd:maxLength value="10" />
                  </xsd:restriction>
                </xsd:simpleType>
              </xsd:element>
              <xsd:element name="ACCNO_Start" nillable="1">
                <xsd:simpleType>
                  <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1028" sqltypes:sqlCompareOptions="IgnoreKanaType IgnoreWidth">
                    <xsd:maxLength value="50" />
                  </xsd:restriction>
                </xsd:simpleType>
              </xsd:element>
              <xsd:element name="ACCNO_End" nillable="1">
                <xsd:simpleType>
                  <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1028" sqltypes:sqlCompareOptions="IgnoreKanaType IgnoreWidth">
                    <xsd:maxLength value="50" />
                  </xsd:restriction>
                </xsd:simpleType>
              </xsd:element>
              <xsd:element name="UptDate" type="sqltypes:datetime" nillable="1" />
              <xsd:element name="UPTUSER" nillable="1">
                <xsd:simpleType>
                  <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1028" sqltypes:sqlCompareOptions="IgnoreKanaType IgnoreWidth">
                    <xsd:maxLength value="50" />
                  </xsd:restriction>
                </xsd:simpleType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:schema>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>7</Id>
        <Cate_Code>mi        </Cate_Code>
        <GP>C</GP>
        <COMPANY>egat</COMPANY>
        <DEPT>CegatB2000</DEPT>
        <ACCNO_Start>2222</ACCNO_Start>
        <ACCNO_End>2222</ACCNO_End>
        <UptDate>2010-12-03T17:28:41</UptDate>
        <UPTUSER>2222</UPTUSER>
      </ACCNOSET>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>8</Id>
        <Cate_Code>mi        </Cate_Code>
        <GP>D</GP>
        <COMPANY>eic</COMPANY>
        <DEPT>Deic01000</DEPT>
        <ACCNO_Start>4444</ACCNO_Start>
        <ACCNO_End>4444</ACCNO_End>
        <UptDate>2010-12-03T17:27:24</UptDate>
        <UPTUSER>2222</UPTUSER>
      </ACCNOSET>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>9</Id>
        <Cate_Code>pi        </Cate_Code>
        <GP>B</GP>
        <COMPANY>eis</COMPANY>
        <DEPT>BeisA2000</DEPT>
        <ACCNO_Start>1111</ACCNO_Start>
        <ACCNO_End>11111</ACCNO_End>
        <UptDate>2010-12-03T17:27:53</UptDate>
        <UPTUSER>2222</UPTUSER>
      </ACCNOSET>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>10</Id>
        <Cate_Code>mi</Cate_Code>
        <GP>C</GP>
        <COMPANY>egat</COMPANY>
        <DEPT>CegatB2000</DEPT>
        <ACCNO_Start>9999</ACCNO_Start>
        <ACCNO_End>999999</ACCNO_End>
        <UptDate>2011-01-06T16:06:24</UptDate>
        <UPTUSER>jim1</UPTUSER>
      </ACCNOSET>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>11</Id>
        <Cate_Code>pi</Cate_Code>
        <GP>B</GP>
        <COMPANY>eis</COMPANY>
        <DEPT>BeisA2000</DEPT>
        <ACCNO_Start>1254846</ACCNO_Start>
        <ACCNO_End>43654543</ACCNO_End>
        <UptDate>2011-01-06T16:06:49</UptDate>
        <UPTUSER>jim1</UPTUSER>
      </ACCNOSET>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>12</Id>
        <Cate_Code>gi</Cate_Code>
        <GP>C</GP>
        <COMPANY>egat</COMPANY>
        <DEPT>CegatB2000</DEPT>
        <ACCNO_Start>4548</ACCNO_Start>
        <ACCNO_End>4021</ACCNO_End>
        <UptDate>2010-12-23T17:44:32</UptDate>
        <UPTUSER>jim1</UPTUSER>
      </ACCNOSET>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>13</Id>
        <Cate_Code>gi</Cate_Code>
        <GP>D</GP>
        <COMPANY>eic</COMPANY>
        <DEPT>Deic01000</DEPT>
        <ACCNO_Start>1232</ACCNO_Start>
        <ACCNO_End>1562</ACCNO_End>
        <UptDate>2010-12-23T19:49:09</UptDate>
        <UPTUSER>jim1</UPTUSER>
      </ACCNOSET>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>14</Id>
        <Cate_Code>gi</Cate_Code>
        <GP>D</GP>
        <COMPANY>eic</COMPANY>
        <DEPT>Deic01000</DEPT>
        <ACCNO_Start>10</ACCNO_Start>
        <ACCNO_End>30</ACCNO_End>
        <UptDate>2011-01-06T16:43:09</UptDate>
        <UPTUSER>jim1</UPTUSER>
      </ACCNOSET>
      <ACCNOSET xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <Id>15</Id>
        <Cate_Code>ma</Cate_Code>
        <GP>C</GP>
        <COMPANY>egat</COMPANY>
        <DEPT>CegatB2000</DEPT>
        <ACCNO_Start>100</ACCNO_Start>
        <ACCNO_End>200</ACCNO_End>
        <UptDate>2011-02-20T17:03:40</UptDate>
        <UPTUSER>jim1</UPTUSER>
      </ACCNOSET>
    </root>'
    使用这样去读取数据时,不能读到值
    select B.v.query('text()') from @xml.nodes('root/ACCNOSET/*') B(v)
      

  4.   

    如果生成的xml生成存在xsd的话,按照原本的方式读取数据有些问题。不信可以试试