Blog By Hal Hayes
Saturday, March 22, 2008

I found this great link that describes international address formats. I will be using this information to help generate some realistic XML content in an upcoming article for MS SQL Tips.

BitBoost Systems International Mailing Address Formats

3/22/2008 12:14:46 PM (Eastern Daylight Time, UTC-04:00) |  | XML#
Wednesday, December 12, 2007

Microsoft reaches standardization with the W3C XQuery recommendation with the addition of the LET clause in the next release of SQL Server, SQL Server 2008.

Here is an example of using the LET clause as part of the XQuery syntax.

CREATE TABLE customerData(

       ID int IDENTITY(1,1) NOT NULL,

       customerDocs xml NOT NULL,

       updated datetime NOT NULL DEFAULT (getdate()),

 CONSTRAINT [PK_customerData] PRIMARY KEY CLUSTERED

(

       [ID] ASC

) ON [PRIMARY]

) ON [PRIMARY]

GO

 

 

INSERT INTO customerData(customerDocs)

       VALUES(N'<?xml version="1.0"?>

       <customers>

              <customer FirstName="Bob" LastName="Hayes" Zipcode="91126" status="current">

                     <order ID="12221" Date="July 1, 2006">Laptop</order>

              </customer>

              <customer FirstName="Judy" LastName="Amelia" Zipcode="23235" status="current">

                     <order ID="12221" Date="April 6, 2006">Workstation</order>

              </customer>

              <customer FirstName="Howard" LastName="Golf" Zipcode="20009" status="past due">

                     <order ID="3331122" Date="December 8, 2005">Laptop</order>

              </customer>

              <customer FirstName="Mary" LastName="Smith" Zipcode="12345" status="current">

                     <order ID="555555" Date="February 22, 2007">Server</order>

              </customer>

       </customers>')

GO

 

-- FLWOR with LET operator

SELECT customerDocs.query('

       <CustomerOrders> {

       for $i in //customer

       let $name := concat($i/@FirstName, " ", $i/@LastName)

       order by $i/@LastName

       return

              <Customer Name="{$name}">

              {

              $i/order

              }

              </Customer>

       }

       </CustomerOrders>

')

FROM customerData

 

12/12/2007 9:19:17 AM (Eastern Standard Time, UTC-05:00) |  | SQL Server 2005 | XML | XQuery#

It would be great to create a library of XQuery strings maintained in the database that developers could use and DBAs could manage. Unfortunately, you are going to have to jump through some hoops to make that happen in SQL Server 2005. The problem originates from the fact that the XML datatype "query" method can only take a literal string value.

 

This also causes problems if you want to use dynamic queries. You can create a SQL statement involving a dynamic query, but what you have to do is piece together the entire query as a string that is called in the dynamic SQL stored procedure "sp_executesql". But, you cannot pass the XQuery as a variable in dynamic queries either.

 

Below are some examples of what works and what does not.

 

The first example shows a typical XQuery call, then one that will not work that uses a variable passed to the query method.

 

-- This works!

DECLARE @myResult xml;

 

SELECT @myResult = Study.query(

 '

  <Root> {

  for $f in //Form

  where $f/@Status eq "COMPLETE"

  return

       <Forms> {

              $f

              }

       </Forms>

} </Root>

 '

)

FROM JustXML

WHERE ID = 1;

 

SELECT @myResult;

GO

 

-- This does not (PASSING VARIABLE INSTEAD OF STRING LITERAL)

 

DECLARE @myResult xml;

DECLARE @myXquery nvarchar(500);

 

SET @myXquery =  '

  <Root> {

  for $f in //Form

  where $f/@Status eq "COMPLETE"

  return

       <Forms> {

              $f

              }

       </Forms>

} </Root>

 ';

 

 

SELECT @myResult = Study.query(@myXquery)

FROM JustXML

WHERE ID = 1;

 

SELECT @myResult;

GO

 

 

Here are two examples, one that works and one that does not, using dynamic SQL queries.

 

-- Using Dynamic Queries with XQuery

-- ---------------------------------

-- THIS WORKS (dynamic sql)

DECLARE @myXquery nvarchar(255);

 

SET @myXquery =  N'

  <Root> {

  for $f in //Form

  where $f/@Status eq "COMPLETE"

  return

       <Forms> {

              $f

              }

       </Forms>

} </Root>

 '

DECLARE @myDynamicSql nvarchar(500);

 

SET @myDynamicSql = N'SELECT Study.query(''' + @myXquery +

       ''')  AS MYRESULT

FROM JustXML

WHERE ID = 1'

 

EXECUTE sp_executesql  @myDynamicSql;

 

-- But this does not!!!

DECLARE @myXquery nvarchar(255);

 

SET @myXquery =  N'

  <Root> {

  for $f in //Form

  where $f/@Status eq "COMPLETE"

  return

       <Forms> {

              $f

              }

       </Forms>

} </Root>

 '

DECLARE @myDynamicSql nvarchar(500);

DECLARE @ParmDefinition NVARCHAR(500);

 

 

SET @myDynamicSql =

N'SELECT Study.query(@thisXquery)  AS MYRESULT

FROM JustXML

WHERE ID = 1'

SET @ParmDefinition = N'@thisXquery nvarchar(255)';

 

 

EXECUTE sp_executesql  @myDynamicSql, @ParmDefinition,

                       @thisXquery = @myXquery;

 

12/12/2007 9:00:05 AM (Eastern Standard Time, UTC-05:00) |  | SQL Server 2005 | XML | XQuery#
Friday, November 30, 2007

For those who attended my recent presentation, "XQuery for DBAs and Developers", at NovaSQL, you can find the files here.

 

NovaSQLNov2007.zip (958.63 KB)

 

NovaSQL is the Northern Virginia SQL Server User Group. I would like to thank Jim Rotan and Jeremy Kadlec for the invitation to speak.

11/30/2007 9:07:43 AM (Eastern Standard Time, UTC-05:00) |  | NovaSQL | SQL Server 2005 | XML | XQuery#
Thursday, October 18, 2007

OK, I'm a little slow. But, here are the two presentations I made at the Richmond Code Camp two weeks ago. These are in Microsoft Powerpoint.

XQuery for DBAs and Developers (Powerpoint)

Richmond Code Camp XQuery for DBAs and Developers.pptx (980.39 KB)

XQuery SQL Demonstration File (.SQL)

XQuery Presentation.sql (42.59 KB)

Dr. Jekyll and Mr. Hyde; SQLCLR for DBAs and Developers (Powerpoint)

SQLCLR Richmond Code Camp Oct 6.pptx (620.63 KB)

10/18/2007 9:54:40 AM (Eastern Daylight Time, UTC-04:00) |  | .NET Framework 2.0 | Code Camp | SQL Server 2005 | SQLCLR | UDA | XML | XQuery#
Friday, October 05, 2007

Well, I have really done it now. I'm giving two presentations tomorrow at the Richmond CodeCamp. Andy Leonard is putting on another show, and if this one is as good as the last, everyone coming should be in for a real treat.

 

See you all Saturday!

10/5/2007 9:37:21 PM (Eastern Daylight Time, UTC-04:00) |  | Code Camp | SQLCLR | Visual Studio 2005 | XML | XQuery#
Monday, April 30, 2007

 

Located below are the SQL, data and VB project files from my presentation at the Richmond Code Camp

RichmondCodeCampXquery.zip (12.6 KB)

Here is my powerpoint presention. It is more than a little light because the focus of CodeCamp is the CODE!

Richmond Code Camp XQuery Tips and Tricks.ppt (2.34 MB)

I would like to thank Andy Leonard, Susan Lennon, and the rest of the Richmond crew for a well run operation.

4/30/2007 9:45:26 PM (Eastern Daylight Time, UTC-04:00) |  | .NET Framework 2.0 | Code Camp | Programming | SQL Server 2005 | Visual Studio 2005 | XML | XQuery#
Wednesday, April 25, 2007

When working on code developing a SQLCLR stored procedure for my upcoming presentation at the Richmond Code Camp, I ran into this problem with the SqlPipe output.

Here is my code:

<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub procExtractSubjectDetail ()
'Return a string of Subject IDs/XML seperated by a ";"
Try
'Connect to database, notice the context connection
Dim Command As SqlCommand = New SqlCommand
Command.Connection = New SqlConnection("Context connection=true")
Command.Connection.Open()

'Generate the SQL query
Dim strSQL As String = "SELECT ID, Study FROM StudyDetail"

'Attach query to command object
Command.CommandText = strSQL

'Execute command object and get results back in a SQL reader object
Dim reader As SqlDataReader = Command.ExecuteReader()

'Create an efficient stringbuilder to hold the results
Dim Subjects As StringBuilder = New StringBuilder

'Add an emtpy string
Subjects.Append("")

'Collect the results
While reader.Read
'String output size limit is 4kb
'so let us restrict our output for this demo
Subjects.Append(reader.Item(0).ToString() + "/")
Dim sXml As SqlXml = reader.GetSqlXml(1)

Subjects.Append(sXml.Value.ToString)
End While

'Close and release the reader
reader.Close()
reader = Nothing

Dim strResult As String = Subjects.ToString()

'Is it too big?
Dim iSize As Integer = strResult.Length

'Send the results
SqlContext.Pipe.Send(strResult)

Command.Connection.Close()

Catch ex As Exception
'Catch the error and resend it. You could add more error code handling here.
Throw New ApplicationException("An error occurred. " + ex.ToString())
End Try

End Sub

--

This was failing when I ran the code. The issue? SqlContext.Pipe.Send(STRING) only allows a string size of 4k bytes. My output was over 7k.

4/25/2007 1:05:28 PM (Eastern Daylight Time, UTC-04:00) |  | Code Camp | Programming | SQL Server 2005 | XML | XQuery | SQLCLR#
Tuesday, February 13, 2007

XQuery has now reached standardization by the W3C. The XML Team at Microsoft is now asking for input on whether there should be a standalone XQuery implementation in the .Net Framework in their blog entitled Standalone XQuery Implementation in .NET?.

 

My posted response is below:

I, also, would like to see an XQuery implementation within the .Net Framework.

If available, I would be more inclinded to use XQuery over XSLT, particularly in a more dynamic setting to extract data, and then to shape the output before consuming within an application.

While the XQuery implementation in SQL Server 2005 is excellent, it is still only a subset of the standard, for example, the ability to query against multiple documents. And, I would prefer to see the implementation as native to the .Net Framework, rather than as a commercial add-on.

 

The differences between what could be and what is available now are two fold. First, the current implementation resides on server-side, within the SQL Server itself. A .Net Framework (System.XML.XQuery) implementation would allow the developer to choose where the query would be executed. Additionally, you would be able to query across multiple documents, which you cannot do in the SQL Server 2005 version.

I have found the XQuery syntax to be easier to craft to extract XML content and shape the output than using XSLT. I think there is a strong incentive to have the XQuery implementation because it can provide some features that may either be missing or are easier to use than SQL Server XQuery, LINQ or XSLT.

I vote "Yes" to XQuery in the .Net Framework.

 

 

2/13/2007 6:07:35 PM (Eastern Standard Time, UTC-05:00) |  | .NET Framework | Programming | SQL Server 2005 | Standards | XML | XQuery#
Friday, September 01, 2006

I gave a presentation on XQuery at the NovaSQL user group that meets in Vienna, Virginia.

Jeremy Kadlec and Brian Moran run a great program. The turnout was very good, too. Lots of SQL experts.

Do you know how you can tell if you are giving a presentation to people that are SQL experts? You can tell that they are when you try to do a SELECT statement, and you get the dreaded Invalid object name error. Before you can blink you hear twenty-thirty voices saying, "You're in the Master database!"

I had a great deal of fun talking about XQuery with a very savvy crowd.

Attached to this post are my PowerPoint slides from the presentation.

 

XQuery-NovaSQL.ppt (106 KB)
9/1/2006 6:10:43 PM (Eastern Daylight Time, UTC-04:00) |  | Microsoft | NovaSQL | SQL Server 2005 | Standards | XML | XQuery#
Monday, October 03, 2005
Using Altova's newly released AltovaXML engine, here is a demonstration of building an XQuery statement processor using Visual Basic .NET.
10/3/2005 3:47:35 PM (Eastern Daylight Time, UTC-04:00) |  | .NET Framework | Visual Basic | XML | XQuery#
Wednesday, September 21, 2005

OK, this is my own personal opinion, but I think it is in Microsoft's best interest to have people on standards committees and working groups, like the World Wide Web (W3C).

I imagine that it might be uncomfortable for Microsoft and its managers to have staff that both pursue work on Microsoft projects and also work on standards committes. Not only is there the issue of dividing time with your valuable resources (i.e. your employee), but also there may be times when the standards work against Microsoft.

However, that being said, as a programmer and developer, I am more apt to embrace technologies that work with open standards - not necessarily open source - then those that don't. Politically, it is a good move for Microsoft - I think it helps with acceptance of the product and tools. And, on a personal level, I hold in high esteem those folks that are working hard to come up with the standards so that systems can be built that are flexible, consistent, and can communicate. I imagine that it is not easy to work in such a compromising environment, but i am confident that the results will yield better tools for us developers.

Like it or not, it is the developers, and others down at the code or hardware level, who will always be the evangelists for technologies. I've rarely ever met a manager that was. If Microsoft relies on managers to be its guide, then it will lose its passion and the passion of its developer community, and become just another company. But "playing nice" in a cooperative manner with standards groups is one way to keep that passion alive...and I applaud Microsoft for its involvement and hope they continue to do so.

9/21/2005 9:15:57 AM (Eastern Daylight Time, UTC-04:00) |  | .NET Framework | Programming | XML | Standards#
Thursday, September 08, 2005

So, I didn't make TechEd, but I'm on my way to Microsoft's PDC.

I'm looking forward to seeing what's going on, especially in the XQuery area. Michael Rys is giving the following presentation which I'm hoping to attend:

DAT405  SQL Server 2005: Deep Dive on XML and XQuery

You can find out more about PDC here. Sorry, it is already sold out.

9/8/2005 11:09:46 AM (Eastern Daylight Time, UTC-04:00) |  | PDC | SQL Server 2005 | Visual Studio 2005 | XML | XQuery#
Thursday, August 11, 2005
Altova, maker of XMLSpy, announces royalty free XML engine component.
8/11/2005 10:48:31 AM (Eastern Daylight Time, UTC-04:00) |  | .NET Framework | XML | XQuery#
Search
Archive
Links
Categories