Today I went to review the SQL Server 2008 enhancements on XML support. To test the queries on AdventureWorks, the first thing I needed to know was: “Where are the XML data columns?”
I don’t actually have the graphic on the database model to search for and wasn’t on the mood to create the database diagram to search visually for the XML columns.
Then, I’ve got the idea of searching on the catalog for that. It should be simple to do (and it was!) but worth the post, as many people may have the need to use the same thing, not only on AdventureWorks, but on large (I mean many tables) databases:
Here is the script:
SELECT t.name, c.name, ty.name
FROM sys.tables t INNER JOIN sys.columns c ON
(t.object_id = c.object_id)
INNER JOIN sys.systypes ty ON
(c.system_type_id = ty.xtype)
WHERE ty.name = ‘xml’
With this script, you can create a procedure that returns the table name and the column name that uses a type that’s passed by a parameter. Or even create a PowerShell function to achieve this. Let’s see the latter:
param([string] $typeName, [string] $server, [string] $database)
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection "server=$server;database=$database;Integrated Security=sspi"
$sqlConnection.Open()
$sqlCommand = $sqlConnection.CreateCommand()
$sqlCommand.CommandText = "SELECT t.name [Table], c.name [Column]
FROM sys.tables t INNER JOIN sys.columns c
ON (t.object_id = c.object_id)
INNER JOIN sys.systypes ty
ON (c.system_type_id = ty.xtype)
WHERE ty.name = ‘$typename ‘"
$sqlReader = $sqlCommand.ExecuteReader()
$dataTable = New-Object System.Data.DataTable
$dataTable.Load($sqlReader);
$sqlConnection.Close()
Write-Output $dataTable
Interesting, huh?
Publicado em Admin, Banco de Dados, Microsoft, PowerShell, SQL Server, System Catalog, Windows Image may be NSFW.
Clik here to view.

Clik here to view.
