Handling XSD dateTime in SQL Server 2005

November 27, 2008

A client is running an application which exchanges XML documents with other installations of the same application. The application’s documentation also includes XSD schema definitions.

What an opportunity for me to get to study SQL Server 2005’s XML capabilities.

I open the .xsd files in a text editor, and invoke CREATE XML SCHEMA COLLECTION statements for each xsd file.

Next, I create a table which contains the XML data originally stored as ntext, and an untyped XML column. Additionally, each XSD gets a separate typed XML column:

CREATE TABLE [dbo].[XmlTable](
    [OID] [int] IDENTITY(1,1) NOT NULL,
    [Data] [ntext] NULL,
    [XmlData] [xml] NULL,
    [XsdData1] [xml] (CONTENT [dbo].[Xsd1] NULL
    ...
)

I import data from the production system into the Data column, and copy the data as untyped XML using

UPDATE XmlTable SET XmlData = Data

If every record contains valid XML data, the operation should complete successfully, with XmlData now containing the (untyped) XML documents. Remember that the Data column starts with an XML tag identifying the XSD. We can now start to copy the data to the typed (XSD-based) XML columns:

UPDATE XmlTable SET XsdData1 = XmlData WHERE Data LIKE '<Xsd1%'

which works fine unless you have an xsd:dateTime value in a format that SQL Server cannot process, generating an error message

XML Validation: Invalid simple type value

As this MS feedback page explains, SQL Server 2005 cannot handle datetime values with timezones. An xsd:dateTime value therefore has to end with a ‘Z’.

To make the XML data match SQL Server’s interpretation of the XSD schema information, we need to replace each xsd:dateTime from yyyy-mm-ddThh:mm:ss.sssssss+hh:mm (day, time, second fractions, timezone offset) to yyyy-mm-ddThh:mm:ssZ.

So, first find all XSD columns of type xsd:dateTime (Michael’s post was very inspiring):

SELECT sc.name XSD, sel.name Element
FROM sys.xml_schema_component_placements scp
INNER JOIN sys.xml_schema_types sct
    ON scp.placed_xml_component_id=sct.xml_component_id
INNER JOIN sys.xml_schema_elements sel
    ON scp.xml_component_id = sel.xml_component_id
INNER JOIN sys.xml_schema_collections sc
    ON sel.xml_collection_id = sc.xml_collection_id
WHERE sct.name = 'dateTime' AND sct.xml_namespace_id = 1
ORDER BY 1, 2

The resultset contains the XML Schema Collection and Element names.

Next we take any one of the retrieved elements and check their values in the imported XmlData column using XQuery syntax:

SELECT XmlData.value('(//TimeStamp)[1]', 'nvarchar(50)')
FROM XmlTable

The pattern of date and time requires 19 character.

Using XMLDML, we can now update the timestamp value by taking the first 19 characters and appending ‘Z’ using UPDATE .modify. The result is calculated using XQuery functions.

UPDATE XmlTable
SET XmlData.modify(
    'replace value of (//TimeStamp/text())[1]
     with concat(substring((//TimeStamp)[1], 1, 19), "Z")')

After this step is repeated for each dateTime XML element, the XML content can be inserted into the typed XML columns, as stated above:

UPDATE XmlTable SET XsdData1 = XmlData WHERE Data LIKE '<Xsd1%'

Generating Wiki Documentation from Database Schema

November 23, 2008

The first public version of dbscript already had the functionality to generate XML documents from database schema information.

I extended this functionality to apply stored XSL files to the generated XML. Thus, it is possible to transform table information from XML into MediaWiki markup, or any other text-based markup.

See here for a screenshot of the web application, and the result documenting a table used by dbscript.

An evaluation version of dbscript is available for download here.


dbscript Screenshots

November 18, 2008

I published a couple of screenshots of my new web application, dbscript:

  • List of versions in a project
  • Run check routines on the datamodel of a project version
  • Generate CREATE scripts
  • Definition of a script to export table data
  • Generate a procedure to export table data
  • Generate table data from a connected database
  • Definition of a script to generate C# constants
  • Generated C# constants
  • Result of comparing database schemas
  • Defining the script of a database schema migration
  • Generating a database schema migration script
  • Storing and editing T-SQL code snippets
  • Executing a T-SQL code snippet on a connected database.

The screenshots are available here.


Introducing dbscript

November 13, 2008

I mentioned in some of my previous posts, that I developed a database tool that I use for the development of other projects.

Now I find it is time to release it to the public, and I name it… dbscript.

The main functionality covers:

  • Import and upload of database schemas
  • Scripting imported schemas
  • Scripting table values of databases
  • Generating scripts to script table values
  • Generating C# const or enum definitions based on table data
  • Comparing database schemas
  • Generating a database schema migration script from these comparisons
  • Generating XML-based documentation of database objects
  • Storing T-SQL code snippets which can be run on accessible databases

dbscript is an ASP.Net web application with an SQL Server database.

It requires IIS, .Net 2.0 with Ajax Control Toolkit installed, and an SQL Server 2000 or 2005 database.

Since my main software projects usually require 2000 functionality, it is tested on SQL Server 2000, but also covers SQL Server 2005.

Since support for SQL Server 2005-specific objects is not complete, it is not a 1.00 release; rather I chose version 0.91 to indicate its functionality is very close to a version 1.

An evaluation version of dbscript (restricted to 10 schemas) is available for download. See online help for installation instructions.