Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Cara Mengekstrak data dari kolom xml di sql 2008

declare @T table
(
  XMLCol xml
)

insert into @T values
('<Menu>
    <Id>1</Id>
      <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-1.jpg</Url>
  </Menu>
  <Menu>
    <Id>2</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-2.jpg</Url>
  </Menu>
  <Menu>
    <Id>3</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-3.jpg</Url>
  </Menu>
  <Menu>
    <Id>4</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-4.jpg</Url>
  </Menu>
  <Menu>
    <Id>5</Id>
    <Url>http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-5.jpg</Url>
  </Menu>')

select X.N.value('Id[1]', 'int') as Id,
       X.N.value('Url[1]', 'varchar(max)') as Url
from @T as T
  cross apply T.XMLCol.nodes('/Menu') as X(N)

Hasil:

Id          Url
----------- ---------------------------------------------------------------------
1           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-1.jpg
2           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-2.jpg
3           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-3.jpg
4           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-4.jpg
5           http://www.zmtcdn.com/menus/1211/menu-photo-for-barbeque-nation-5.jpg


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL pilih semua jika parameternya nol, jika tidak, kembalikan item tertentu

  2. Kapan SQL Server Mengurutkan Mundur?

  3. Buat Pemicu di SQL Server

  4. Mvc-Mini-Profiler v1.7 pada proyek EF 4.1 Code-First tidak memprofilkan SQL

  5. Skalar UDF Inlining di SQL Server 2019