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
| |
|
|
|
|
|
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;
| |
|
|
|
|
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. |
|
|
|
|
Thursday, October 18, 2007 |
|
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! |
|
|
|
|
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. |
|
|
|
|
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.
|
|
|
|
|
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) |
|
|
|
|
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. |
|
|
|
|
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. |
|
|
|
|
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. |
|
|
|
|
Thursday, August 11, 2005 |
|
|
Altova, maker of XMLSpy, announces royalty free XML engine component. |
|
|
|
|
|
|
|
| Archive |
| October, 2008 (1) |
| September, 2008 (3) |
| August, 2008 (1) |
| July, 2008 (1) |
| June, 2008 (4) |
| May, 2008 (1) |
| April, 2008 (5) |
| March, 2008 (7) |
| February, 2008 (1) |
| December, 2007 (6) |
| November, 2007 (4) |
| October, 2007 (3) |
| September, 2007 (1) |
| August, 2007 (4) |
| July, 2007 (1) |
| June, 2007 (1) |
| April, 2007 (5) |
| March, 2007 (7) |
| February, 2007 (4) |
| January, 2007 (2) |
| September, 2006 (2) |
| August, 2006 (1) |
| March, 2006 (3) |
| October, 2005 (3) |
| September, 2005 (10) |
| August, 2005 (3) |
| July, 2005 (2) |
| June, 2005 (4) |
| May, 2005 (12) |
|
|
|
|