Tuesday, January 11, 2005
SQL Server 2005-XML Defaults and XQuery Constraints on an XML Column
The XML Data Type can, just like other data types, conform to Nullability, Defaults and Constraints. If you want to make the field required (NOT NULL) and provide a default value on your XML column, just specify like you would any other column.
CREATE TABLE OrdersXML
(OrderDocID INT PRIMARY KEY,
xOrders XML NOT NULL Default '</Orders>'
This insert will work because it is relying on the default:
Insert Into OrdersXML (OrderDocID, xOrders) Values (2, DEFAULT)
The following insert will work, even if we add non <Orders> in our table because we have not declared a constraint on the column:
Insert Into OrdersXML (OrderDocID, xOrders) Values (3, 'steve')
SQL Server 2005 gives you the opportunity to add a constraint. An interesting feature of an XML column is that the CHECK constraint is based on the XML Infoset model using the methods of the XML column. You can use the exist method, which is specific to the XML type, as part of our CHECK constraint to force a particular type of XML element to exist. In the following example we will drop and recreate the table from scratch (you can use ALTER TABLE as well) adding a default and CHECK CONSTRAINT.
Drop Table OrdersXML
--create the table with the XML Datatype
--using a default value
--also has the check constraints
CREATE TABLE OrdersXML
(OrderDocID INT PRIMARY KEY,
xOrders XML NOT NULL Default '<Orders>'
CONSTRAINT xml_orderconstraint
CHECK(xOrders.exist('/Orders')=1))
Now this example from before will fail.
Insert Into OrdersXML (OrderDocID, xOrders) Values (3, 'steve')
When you attempt to insert data that will violate the xml_orderconstraint you get the following error message from SQL Server:
Msg 547, Level 16, State 0, Line 1
INSERT statement conflicted with CHECK constraint
'xml_orderconstraint'. The conflict occurred in database
'AdventureWorks', table 'OrdersXML', column 'xOrders'.
The statement has been terminated.
Monday, January 10, 2005
Mets, Jets and Marathon Training
The Mets made a spash as did the Jets this weekend, but marathon training is right on schedule, a 5 mile race was ran on Sunday with the usual results (Kathleen beating us). This is the first of 9 races to qualify for the 2006 New York City Marathon. Antarctica marathon is 47 days away...
|
Last Name
|
First Name
|
Sex/ Age
|
Bib
|
Team
|
City
|
State
|
Net Time
|
Pace/ Mile
|
AG Time
|
|
GURBISZ |
KATHLEEN |
F27 |
6802 |
|
NY |
NY |
44:14 |
8:50 |
44:14 |
|
FORTE |
STEPHEN |
M32 |
6782 |
|
NEW YORK |
NY |
44:23 |
8:52 |
44:23 |
|
VAROLI |
LINDA |
F34 |
6978 |
|
NEW YORK |
NY |
45:18 |
9:03 |
45:18 |
Thursday, January 06, 2005
My Cafe in the Netherlands
Since I speak at the SDC in the Netherlands so much (every year since 1997 I think?) I decided to open a cafe in the Netherlands. Here is my buddy Remi Caron after he discovered my cafe.
Tuesday, January 04, 2005
Monday, January 03, 2005
iTunes=Quicktime?
When you want to view a web site that has a Quick Time video and you go to Apple.com to download the free Quick Time player for Windows 2000 or XP, you HAVE to install iTunes. What does iTunes have to do with QuickTime? So many move previews are done in QuickTime and Apple knows this. So they are using their dominance in one area to force something else on us. Somebody call the US Justice Department.
Anyway Apple owns the iPod market, but a years worth of iPod sales is equal to 2 weeks of Dell shipments…
Tuesday, December 28, 2004
Call a spade a spade
Osama bin Laden formed an official alliance with the Iraqi terrorist Sunni Zarqawi yesterday and urged the minority Sunnis to protest the Jan 30th election in Iraq.
Previously bin Laden (who is a Sunni Muslim) has not been a vocal enemy against the Shiites. Remember Saddam was a minority Sunni kept in power by force and repressed the majority Shiite population. (As well as the minority Kurds.) By forming an official alliance with Zarqawi, bin Laden’s now pitting himself against the Shiite majority. (Like the good Wahhabist he really is.)
So January 30th there will be elections with or without the Sunnis. Osama bin Laden said anyone who will vote is an infidel. Just like in Afghanistan where the radical Sunni Taliban allied with bin Laden are still in their holes protesting the first freely elected leader in Afghanistan’s history, Zarqawi’s Sunni Muslims will just get more radical and blood thirsty with an alliance with bin Laden, boycott elections and live in their holes. Just like in Afghanistan, moderate Sunnis (like the moderate Sunni non Wahhabist Taliban) did not align themselves with terror and wanted to be part of the political process and laid down their arms and formed political opposition parties. Moderate Sunnis will do the same in Iraq. Sure some will stay home, but they know that the majority of the population made up of Kurds and Shiites (both previously gassed by Saddam) would rather die then not vote and will not risk staying out of the process. The Sunnis can see the writing on the wall.
Will Zarqawi’s terror continue? Unfortunately yes. How do we stop them? Hold free elections January 30th and give the people a stake in their government. Zarqawi’s Sunni “insurgent” group is not an “insurgency” but just a radical group of terrorists opposed to democracy-as his alliance with bin Laden now proves. Let’s now call a spade a spade and label his group terrorists. His legitimacy has sunk even lower with moderate Sunnis with the bin Laden alliance. As democracy grows in Iraq over time Zarqawi’s group will diminish. It will take time, maybe years as it did in Afghanistan.
Lastly, we need not worry about Iran, the Shiite power next door. Elected moderate Iraqi Shiites will reject the radical non democratic and non Arab (Iranians are not Arabs, Iraqis are). Moderate Sunnis will come to the forefront and Zarqawi’s Sunni terrorists will eventually be isolated and marginalized, just like the radical Taliban were in Afghanistan. It took only three years in Afghanistan-give Iraq time.
Fast forward 10 years from now. A democratically elected free Afghanistan, Palestine and Iraq.
Wednesday, December 22, 2004
SQL Server 2005 (Yukon) Bulk Rowset Provider (OPENROWSET Enhancement)
SQL Server 2005 introduces an enhancement to the OPENROWSET function, the new BULK rowset OLE DB provider. Via OPENROWSET, it lets you access data in files in a relational fashion. In the past, you used BULK INSERT to pull data from the file into a temp table and then ran your relational operations, but with SQL Server 2005, you can use the data contained in the file as part of your SQL statement rowset results. Keep in mind that you must specify a format file, which is the same format file you use with bcp.exe or the BULK INSERT statement. The following code shows how to access a file named c:\bulk.txt using the format file c:\bulk.fmt:
SELECT customerid, customername, totalorders
FROM OPENROWSET(BULK 'c:\bulk.txt',
FORMATFILE = 'c:\bulk.fmt') AS (customerid, customername, totalorders)
Tuesday, December 21, 2004
SQL Server 2005 (Yukon)-Recursive Query
SQL Server 2005 adds a new feature called Common Table Expressions (CTE). The true power of CTEs emerges when you use them recursively to perform hierarchical queries on tree structured data. In fact, besides SQL-92 compliance, this was the main reason Microsoft built CTEs. A recursive CTE is constructed from a minimum of two queries, the first, or anchor member (AM), is a nonrecursive query, and the second, or recursive member (RM), is the recursive query. Within your CTE’s parentheses (after the AS clause), you define queries that are either independent or refer back to the same CTE. The AM and RM are separated by a UNION ALL statement. Anchor members and are invoked only once and are invoked repeatedly until the query returns no rows. Multiple AMs can be appended to each other using either a UNION or a UNION ALL operator, depending on whether you want to eliminate duplicates. (You must append recursive members using a UNION ALL operator.) Here is the syntax:
With SimpleRecursive( field names)
As
(
<Select Statement for the Anchor Member>
Union All
<Select Statement for the Recursive Member>
)
Select * From SimpleRecursive
To demonstrate this feature, I will create an example here. We create a table with employees and a self referencing field back to Employee_ID called ReportsTo (I call this a Domestic Key in lue of a Foreign Key). We are going to write a query that returns all the employees who report to Stephen (Employee_ID=2) and all the employees who report to Stephen’s subordinates:
--create a table with tree data
--Reportsto is a "domestic key" back to Employee_id
create table Employee_Tree (Employee_NM nvarchar(50), Employee_ID int Primary Key, ReportsTo int)
--insert some data, build a reporting tree
insert into employee_tree values('Richard', 1, 1)
insert into employee_tree values('Stephen', 2, 1)
insert into employee_tree values('Clemens', 3, 2)
insert into employee_tree values('Malek', 4, 2)
insert into employee_tree values('Goksin', 5, 4)
insert into employee_tree values('Kimberly', 6, 1)
insert into employee_tree values('Ramesh', 7, 5)
Our table looks like this:
|
Employee_NM |
Employee_ID |
ReportsTo |
|
Richard |
1 |
null |
|
Stephen |
2 |
1 |
|
Clemens |
3 |
2 |
|
Malek |
4 |
2 |
|
Goksin |
5 |
4 |
|
Kimberly |
6 |
1 |
|
Ramesh |
7 |
5 |
Now the recursive query to determine all the employees who will report to Stephen:
--Recursive Query
WITH SimpleRecurvice(Employee_NM, Employee_ID, ReportsTO)
AS
(SELECT Employee_NM, Employee_ID, ReportsTO
FROM Employee_Tree WHERE Employee_ID = 2
UNION ALL
SELECT p.Employee_NM, p.Employee_ID, p.ReportsTO
FROM Employee_Tree P INNER JOIN
SimpleRecurvice A ON A.Employee_ID = P.ReportsTO
)
SELECT Employee_NM FROM SimpleRecurvice
Employee_NM
--------------------------------------------------
Stephen
Clemens
Malek
Goksin
Ramesh
(5 row(s) affected)
This recursion starts where Employee_ID= 2 (the ANCHOR MEMBER or the first SELECT). It picks up that record and then, via the RECURSIVE MEMBER (the SELECT after the UNION ALL), picks up all of the records that report to Stephen and that record’s children (Goksin reports to Malek and Malek reports to Stephen). Each subsequent recursion tries to find more children that have as parents the employees found by the previous recursion. Eventually the recursion returns no results and that is what causes the recursion to stop (the reason why Kimberly is not returned).
|