SQL: How can i get the value of an attribute in XML datatype

后端 未结 3 1345
南旧
南旧 2020-12-11 14:55

I have the following xml in my database:


  

I\'m using something like thi

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 15:20

    It depends a lot on how you're querying the document. You can do this, though:

    CREATE TABLE #example (
       document NText
    );
    INSERT INTO #example (document)
    SELECT N'';
    
    WITH XmlExample AS (
      SELECT CONVERT(XML, document) doc
      FROM #example
    )
    SELECT
      C.value('@language', 'VarChar(2)') lang
    FROM XmlExample CROSS APPLY
         XmlExample.doc.nodes('//account') X(C);
    
    DROP TABLE #example;
    

    EDIT after changes to your question.

提交回复
热议问题