Wednesday, January 22, 2020

Tables contains XML columns

 

How to Retrieve Table Names with XML Columns in SQL Databases

When working with SQL databases, it's common to need to find which tables contain XML columns. This can be especially useful for performing certain operations or analyzing database structure.

To help with this, here is a SQL query that can retrieve the table names, along with their schema, where XML columns are present.

SQL Query:


SELECT [Table] = s.name + N'.' + t.name
FROM sys.tables t
   INNER JOIN sys.schemas s ON t.[schema_id] = s.[schema_id]
WHERE EXISTS
(
   SELECT 1 FROM sys.columns c
   WHERE c.[object_id] = t.[object_id]
   AND c.system_type_id = 241 -- 241 = xml
);

This SQL query works by joining the sys.tables and sys.schemas system views to find tables and their schemas. It then filters the tables where any column has a system type ID of 241, which corresponds to the XML data type in SQL Server.


You can learn more about working with XML data in SQL by checking out these related resources:

  1. Using XML Type in SQL Queries
  2. Handling XML Columns with NULL Values

Popular Posts