Re: [SQL ] XML 用node name篩選?

看板Database作者 (可愛小孩子)時間6年前 (2018/02/23 16:24), 編輯推噓1(100)
留言1則, 1人參與, 6年前最新討論串2/2 (看更多)
我參考以下網站來達成你要的: 1. sql xml 入門:xpath和xquery: https://goo.gl/EQ2XiF 2. 節點的本機名稱的函式 https://goo.gl/LusmLN 3. Removing XML tags from FOR XML PATH https://goo.gl/M7Ufz9 ---------------------- SQL 如下: ;with tb1 as ( select tab.col.value('../@id','int') as 'id', tab.col.query('.') as content from @xml.nodes('/row/*') as tab(col) inner join table t1 on tab.col.value('local-name(.)','varchar(100)') = t1.cola ), tb2 as ( select t1.id,( select content as [*] from tb1 where id = t1.id for xml path('') ) as content from tb1 t1 group by t1.id ) select id as '@id',cast(content as xml) as [*] from tb2 for xml path('row') ------------------------------------------ 想法: 1. 觀察 tb1,tb2 就可以知道大概的邏輯了! 2. 想知道還有沒有更好的解法,謝謝大家! ※ 引述《YiMMiY (YiMMiY)》之銘言: : MS SQL - 2012 : 各位版友們好 : 手邊有一個XML,如下 : @xml = ' : <row id='1'> : <AAA>zxc</AAA> : <BBB>asd</BBB> : <CCC>qwe</CCC> : </row> : <row id='2'> : <AAA>vbn</AAA> : <BBB>fgh</BBB> : <CCC>rty</CCC> : <DDD>jkl</DDD> : </row> : <row id='3'> : <AAA>sdf</AAA> : <DDD>xcv</DDD> : </row> : ' : 手邊還有一個TABLE,如下 : Tb_DEF : COLA ... : -------------- : AAA ... : CCC ... : 想達到 : <row id='1'> : <AAA>zxc</AAA> : <CCC>qwe</CCC> : </row> : <row id='2'> : <AAA>vbn</AAA> : <CCC>rty</CCC> : </row> : <row id='3'> : <AAA>sdf</AAA> : </row> : 也就是TABLE的[COLA]為XML的篩選條件 : 請問該如何下指令呢?謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.223.59.47 ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1519374279.A.D1D.html

02/23 17:57, 6年前 , 1F
大感謝啊啊啊 原本我有做到tb1那邊 但tb2那段就做不出來了
02/23 17:57, 1F
文章代碼(AID): #1QZy_7qT (Database)
文章代碼(AID): #1QZy_7qT (Database)