Blog By Hal Hayes
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#
Search
Archive
Links
Categories
Admin Login
Sign In
Blogroll
 CTO 2.0
Antonio Chagoury
Dot NET Ramblings
Brian Noyes
 New Entry
 SharePoint Resources
Lamont Harrington
 Winsmarts
Sahil Malik
Themes
Pick a theme: