Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

How to select value from strongly typed XML

Table Name: TableHasXML
has Column: XML_Column xml(XMLCollectionSchema)

XML data is similar to:
[root xmlns="http://thingswelovetohate.com/schema/2010/PinayProgrammer.xsd" ....]
[row ]
[column1]Things We Love to Hate[/column1]
[column2]http://www.thingswelovetohate.com[/column2]
[/row]
[row]
[column1]Pinay Programmer[/column1]
[column2]http://beingoyen.blogspot.com[/column2]
[/row]
....
[/root]

(yes, I know that was sloppy but blogger wouldn't let me just copy-paste my actual schema. Blogger treats any greater than/less than signs as HTML tags)

XML_Column.query('declare default element namespace "http://thingswelovetohate.com/schema/2010/PinayProgrammer.xsd";
data(/root/row[Column1="Things We Love to Hate"]/Column2)')
FROM TableHasXML

Result = http://www.thingswelovetohate.com

If your XML column or variable doesn't have a namespace then leave out the namespace declaration as well:
XML_Column.query('data(/root/row[Column1="Things We Love to Hate"]/Column2)') FROM TableHasXML

How to know if a string is part of an XML?

I have a table named "TempTable" with an XML column named "XML_Collection". Now I want to know if the string "My Valentine" appears in any of those XML rows.

I used the string manipulator: PATINDEX

SELECT * from TempTable WHERE PATINDEX('%My Valentine%', convert(varchar(max),XML_Collection)) > 0

Important notes:
  • be sure to convert the XML type to varchar(max) first before using any string manipulator functions
  • if PatIndex == 0 then that string pattern does not exist
  • if PatIndex > 0 then that string pattern exists

Select XML Schema Collection type of an XML Column

I spent an afternoon looking for a way to get the XML Schema Collection type of a given column.

I wanted to copy the contents of a table (that has an XML column with a user defines Schema Collection) to another table but I get this error message: Implicit conversion between XML types constrained by different XML schema collections is not allowed. Use the CONVERT function to run this query. 

I understand that I have to CONVERT it to the same XML Schema Collection but how do I do so dynamically?

I ended up resorting to MSDN's SQL forums. The next morning, Peso, an MVP, gives me the answer:

SELECT object_name(ac.object_id) AS TableName,
ac.name as ColumnName,
  x.name AS CollectionName
FROM sys.all_columns AS ac
LEFT JOIN sys.xml_schema_collections AS x ON x.xml_collection_id = ac.xml_collection_id
WHERE object_name(ac.object_id) = 'MyTableNameHere'

TSQL Error: EXECUTE permission denied on XML Column

I got this error when I created a restricted login for a user. We've only recently been optimizing the use of XML data types in SQL Server so it took me an hour to find the solution -- not on MSDN but on some other programmer's blog (gave my thanks of course! and the link appears on the bottom of this post).

Error we get:
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The EXECUTE permission was denied on the object {XMLSchema1}, database {DatabaseName}, schema 'dbo'.

Solution was simple:
GRANT EXECUTE ON XML SCHEMA COLLECTION::{XMLSchema1} TO {RestrictedLoginName}

Thanks to Bob Beauchemin.