<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Louis Davidson</title><subtitle type="html" /><id>http://sqlblog.com/blogs/louis_davidson/atom.aspx</id><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/default.aspx" /><link rel="self" type="application/atom+xml" href="http://sqlblog.com/blogs/louis_davidson/atom.aspx" /><generator uri="http://communityserver.org" version="2.1.61129.1">Community Server</generator><updated>2007-11-04T15:53:59Z</updated><entry><title>2009 SQL Resolutions</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2009/01/04/2009-sql-resolutions.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2009/01/04/2009-sql-resolutions.aspx</id><published>2009-01-05T02:55:02Z</published><updated>2009-01-05T02:55:02Z</updated><content type="html">&lt;p&gt;Every year I put out resolutions, but this year is a little different.&amp;#160; I will post my SQL only blog resolutions here, and my personal ones &lt;a href="http://drsql.spaces.live.com" target="_blank"&gt;here&lt;/a&gt;. Every year I find my ability to put out stuff is limited by my desire for sleep and fun, and this year is no different. So I am going to be realistic this time…I think.&lt;/p&gt;  &lt;p&gt;1. Only collaborative works this year.&amp;#160; I am collaborating with Tim Ford (&lt;a title="http://ford-it.com/" href="http://ford-it.com/"&gt;http://ford-it.com/&lt;/a&gt;) on the oft mentioned DMV book for red-gate.&amp;#160; I feel it will get done with or without me this time, but I am committed to making it happen.&amp;#160; The MVP book work is mostly done, but there is a good bit more work to do before it goes to print for sure.&lt;/p&gt;  &lt;p&gt;2. At least one SQL blog on this site every two weeks or more often.&amp;#160; I have my pillar posts that are in progress, (one is ready for posting in a day or two), and I am going to post some of the DMV stuff along too, most likely.&lt;/p&gt;  &lt;p&gt;3. Back on the forums more steady now that the holidays have passed. I try to stick to 3-5 posts a day, even when I am writing, but it can be laborious.&amp;#160; Luckily the talent that is working the forums has greatly improved over the past three years.&lt;/p&gt;  &lt;p&gt;4. Bring my back catalog of posts from drsql.spaces.live.com over to sqlblog in cleaned up/updated form, as well as posting them to my website drsql.org for future reference as well.&lt;/p&gt;  &lt;p&gt;5. Keeping drsql.org more up to date with stuff I do.&lt;/p&gt;  &lt;p&gt;6. Speak at least 3 times this year (user group (&lt;a href="http://nashville.sqlpass.org" target="_blank"&gt;nashville.sqlpass.org&lt;/a&gt;), Devlink (&lt;a href="http://devlink.net" target="_blank"&gt;devlink.net&lt;/a&gt;), and &lt;a href="http://sqlpass.org" target="_blank"&gt;PASS&lt;/a&gt;, hopefully).&lt;/p&gt;  &lt;p&gt;7. Work with PASS on social media to help make them a solid place to go to meet other SQL nerds, even those on other communities.&lt;/p&gt;  &lt;p&gt;8. Work on my responsibilities with the Nashville SQL User’s group to help us turn a corner to a thriving group.&amp;#160; Right now we have a lot of potential, but need to do a bit more.&lt;/p&gt;  &lt;p&gt;9. Read more. One thing about producing content is that it prohibits you from ingesting content from others.&lt;/p&gt;  &lt;p&gt;See, realistic. &lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=2009 SQL Resolutions&amp;amp;body=Seen on SQLblog.com: %0A%0A%092009 SQL Resolutions%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2009/01/04/2009-sql-resolutions.aspx" target="_blank" title = "Email 2009 SQL Resolutions"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2009/01/04/2009-sql-resolutions.aspx&amp;amp;title=2009+SQL+Resolutions" target="_blank" title = "Submit 2009 SQL Resolutions to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2009/01/04/2009-sql-resolutions.aspx&amp;amp;phase=2" target="_blank" title = "Submit 2009 SQL Resolutions to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2009/01/04/2009-sql-resolutions.aspx&amp;amp;title=2009+SQL+Resolutions" target="_blank" title = "Submit 2009 SQL Resolutions to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2009/01/04/2009-sql-resolutions.aspx&amp;amp;title=2009+SQL+Resolutions" target="_blank" title = "Submit 2009 SQL Resolutions to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2009/01/04/2009-sql-resolutions.aspx&amp;amp;title=2009+SQL+Resolutions&amp;amp;;top=1" target="_blank" title = "Add 2009 SQL Resolutions to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=10961" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>The phases of database design</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/12/16/the-phases-of-database-design.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/12/16/the-phases-of-database-design.aspx</id><published>2008-12-17T04:43:55Z</published><updated>2008-12-17T04:43:55Z</updated><content type="html">&lt;p&gt;Before I get started with the pillars of a well built database, I want to reply (in long form) to a comment on the last post. I see the phases of the project to have five distinct phases (again trying to make memorable lists that an stick in your mind):&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Requirements – &lt;/strong&gt;The process of extracting &lt;strong&gt;what&lt;/strong&gt; needs to be done from the mind of the people you are going to be implementing for&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Design&lt;/strong&gt; – Preparing for implementation by getting a blueprint of what you are going to build&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Implementation&lt;/strong&gt; – The act of creating objects to store, protect and manipulate data&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Tuning&lt;/strong&gt; – Making the code work fast enough while not losing any fidelity.&amp;#160; In other words, if building a program to add 1 + 1, the faster you can get the answer 2 the better. If you can’t guarantee to get back 2 every time you execute it, it doesn’t matter how fast you can get the answer.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Maintenance&lt;/strong&gt; – The dreaded part of the process, where the developer or support people make changes to the system to meet new needs.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I focus most of my writing in the design book on the Design and Implementation phases of the project. Requirements are usually done by the business analyst role, and tuning and maintenance are follow on processes that happen once you get deeper into how the system is actually used.&lt;/p&gt;  &lt;p&gt;Now don’t take this as if I am one of the elitists that don’t consider the reality of usage in the design. I feel that it is a building process and if you get requirements right they will at least hint at usage (and will more likely be pretty specific about what you need to test for), the design will consider tuning in some ways, implementation will produce a flexible product that can be tuned.&amp;#160; Once you have a well built solution you will then have less need for tuning, but you will have your solution in a form that can easily be tuned, and will be very maintainable.&lt;/p&gt;  &lt;p&gt;If you are an agile bigot, you might be screaming at the screen “Waterfall! NOOOOO”.&amp;#160; Well, don’t.&amp;#160; I can’t hear you anyway. I prefer a heavy dose of requirements up front, but that and any other phase can be split up into smaller phases.&amp;#160; But the fact is you have to figure what you want to do (requirements), how you are going to do it (design) and implement the solution in that order, even if you don’t do everything at once. The only downside with agile techniques is that you need programmers with good foresight.&amp;#160; You might be building a program to add values between 0 and 10 to other values between 0 and 10 in the first phase, but later you need to add all real numbers.&amp;#160; You could write an adding program for 10 values like:&lt;/p&gt;  &lt;p&gt;case when @parm1 = 0 and @parm2 = 0 then 0   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; when (@parm1 = 0 and @parm2 = 1) or (@parm1 = 1 and @parm2 = 0) then 1    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; …&lt;/p&gt;  &lt;p&gt;But you certainly cannot use this methodology for larger numbers..And you wouldn’t want to for smaller ones. Creating over simple solutions can be a downfall of an iterative approach when you have no visionary developers. &lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=The phases of database design&amp;amp;body=Seen on SQLblog.com: %0A%0A%09The phases of database design%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/12/16/the-phases-of-database-design.aspx" target="_blank" title = "Email The phases of database design"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/16/the-phases-of-database-design.aspx&amp;amp;title=The+phases+of+database+design" target="_blank" title = "Submit The phases of database design to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/16/the-phases-of-database-design.aspx&amp;amp;phase=2" target="_blank" title = "Submit The phases of database design to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/16/the-phases-of-database-design.aspx&amp;amp;title=The+phases+of+database+design" target="_blank" title = "Submit The phases of database design to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/16/the-phases-of-database-design.aspx&amp;amp;title=The+phases+of+database+design" target="_blank" title = "Submit The phases of database design to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/16/the-phases-of-database-design.aspx&amp;amp;title=The+phases+of+database+design&amp;amp;;top=1" target="_blank" title = "Add The phases of database design to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=10599" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>The N pillars of a well built database?</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/12/09/the-n-pillars-of-a-well-built-database.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/12/09/the-n-pillars-of-a-well-built-database.aspx</id><published>2008-12-09T22:23:00Z</published><updated>2008-12-09T22:23:00Z</updated><content type="html">&lt;P&gt;As I am starting the process of writing my next edition of the database design book (over the next 3+ years) I am starting to try to come up with some catchy way of stating that a database is well designed and implemented.&amp;nbsp; So I started to think of some metaphor and pillars is the best I could do. Catchy? Dunno, but the idea is that without one, the others could fail. &lt;/P&gt;
&lt;P&gt;I figure I will post a blog on each of them first, and then work my way from there.&amp;nbsp; The end goal being a theme that I run through the book, starting in chapter one with a little intro, then relating the pillars through some of the book, but certainly culminating in the final chapter as a way to tie it all together.&lt;/P&gt;
&lt;P&gt;The pillars I came up with are the following 7. The final number may be more or less, but eventually I need to figure a way to make the number have some deep meaning (7 is theologically, the &lt;A target=_blank href="http://en.wikipedia.org/wiki/7_%28number%29"&gt;perfect number&lt;/A&gt;!) but hey, who knows…I have a few years to ruminate on them.&lt;/P&gt;
&lt;P&gt;Design&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Coherent&lt;/STRONG&gt; – cohesive, comprehendible, standards based, names/datatypes all make sense, needs little documentation &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Normal&lt;/STRONG&gt; – normalized as much as possible without harming usability/performance (based on testing) &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Fundamentally Sound&lt;/STRONG&gt; – fundamental rules enforced such that you don’t have to check datatypes, base domains, relationships, etc &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Documented&lt;/STRONG&gt; – Anything that cannot be gather from the previous four is written down and/or diagrammed for others &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Implementation&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Secure&lt;/STRONG&gt; – Users can only see data they are privy to &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Well Performing&lt;/STRONG&gt; – Gives you answers fast &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Encapsulated&lt;/STRONG&gt; – Changes to the structures cause only changes to usage where a table/column directly accessed it &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I can see this being a great little set of things to have in the back of your mind when you are designing, sort of a checklist to see if you meet these different “pillars” of something or other.&amp;nbsp; Clearly it will take more work (and if any of you chimes in with ideas, I promise credit in the acknowledgements :)&lt;/P&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=The N pillars of a well built database?&amp;amp;body=Seen on SQLblog.com: %0A%0A%09The N pillars of a well built database?%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/12/09/the-n-pillars-of-a-well-built-database.aspx" target="_blank" title = "Email The N pillars of a well built database?"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/09/the-n-pillars-of-a-well-built-database.aspx&amp;amp;title=The+N+pillars+of+a+well+built+database%3f" target="_blank" title = "Submit The N pillars of a well built database? to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/09/the-n-pillars-of-a-well-built-database.aspx&amp;amp;phase=2" target="_blank" title = "Submit The N pillars of a well built database? to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/09/the-n-pillars-of-a-well-built-database.aspx&amp;amp;title=The+N+pillars+of+a+well+built+database%3f" target="_blank" title = "Submit The N pillars of a well built database? to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/09/the-n-pillars-of-a-well-built-database.aspx&amp;amp;title=The+N+pillars+of+a+well+built+database%3f" target="_blank" title = "Submit The N pillars of a well built database? to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/12/09/the-n-pillars-of-a-well-built-database.aspx&amp;amp;title=The+N+pillars+of+a+well+built+database%3f&amp;amp;;top=1" target="_blank" title = "Add The N pillars of a well built database? to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=10413" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>Inheritance in Database Design</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/10/15/inheritance-in-database-design.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/10/15/inheritance-in-database-design.aspx</id><published>2008-10-16T00:30:27Z</published><updated>2008-10-16T00:30:27Z</updated><content type="html">&lt;p&gt;As I have been walking around Disney World this week, my mind starts to wander to matters of database design. Sad, perhaps, but I will guess that most people who read this blog do the same much the same thing with whatever technology they are good at when they are relaxing also.&amp;#160; It also may actually have helped me come up with an example for my next book (especially if I can double the size of the book!)&lt;/p&gt;  &lt;p&gt;Examples are the hardest of all parts of writing for me. Thinking of an example that covers a lot of situations as is really hard, but I think that a theme park might actually be the answer (if it is and you are a writer, don’t steal my idea or I will steal it right back :).&amp;#160; It seems like it actually covers most if not all of the situations that are needed:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Good entities (People, places, ideas)&lt;/li&gt;    &lt;li&gt;Rich attributes to describe the entities&lt;/li&gt;    &lt;li&gt;All of the different parts of normalization, including 4nf (show, cast member, location)&lt;/li&gt;    &lt;li&gt;Geography data (to locate all of the different locations)&lt;/li&gt;    &lt;li&gt;Geometry data (finger print biometrics)&lt;/li&gt;    &lt;li&gt;Image data (pictures of attractions, menus, etc)&lt;/li&gt;    &lt;li&gt;Subclasses&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;And many more.&amp;#160; &lt;/p&gt;  &lt;p&gt;But it is the Subclasses that got me thinking. Initially I was thinking that a Location entity would be the top level entity, then attraction, restaurant, shop, etc as subclasses.&amp;#160; But is this really a subclass? Thinking about the AdventureWorks database the other day I realized the reason I dislike that design is the way it has a BusinessEntity entity as a superclass to almost EVERYTHING. A subclassed entity isn’t like a generic object in an object oriented design, but rather it should be more of a specific entity with even more specific information tacked on for specific types.&amp;#160; The point being that you would figure to use the superclass quite often as itself, with the subclass giving you some additional information sometimes, but not as a rule exclusively using the subclass entities.&lt;/p&gt;  &lt;p&gt;So how alike is a roller coaster, or a restaurant?&amp;#160; Seems that the only thing these share is a position on the globe. So instead of a subclassed entity, why not specify standalone entities for each and create an independent entity for position.&amp;#160; This would be a geographic point, and would serve as the entrance of sorts to the math of determining how far apart the different locations are, as well as (possibly) to predict which place a person would be likely to go next.&amp;#160; This position is not really a parent to the items, but more of an interface (though technically I don’t think an interface doesn’t specify storage…)&lt;/p&gt;  &lt;p&gt;So each table would get a relationship to the position entity that would contain all of the different locations of “stuff” that you have. This is not so different from how address is often implemented, except that an attraction shouldn’t need more than one position. It might have a geometric representation of the area it takes up, but I would think that each location would have one single point on the map that would represent it.&amp;#160; Might this get so detailed as to have multiple items per location?&amp;#160; For example, a very large store having multiple entrance points, each so different as to direct a person in a different way?&amp;#160; Sure, and this might be a matter for the other tables to worry with.&amp;#160; Each position would also need to be able to tell the distance from itself to other positions, with knowledge of the terrain.&lt;/p&gt;  &lt;p&gt;Just having an interface would make things easier to use the subclass technique for the things that are common.. Like a queue.&amp;#160; There are different types of queues all around a them park, and several different types. A roller coaster uses several of them itself (a special ticket to get you through fast, a just stand here and wait line, single rider, etc) as does a restaurant (just stand here and wait, and in some cases, reservations, or priority seating).&amp;#160; Each of these queues shares commonality, but at the same time some differences are obvious.&amp;#160; And an actual queue for one ride may use all of the different types.&lt;/p&gt;  &lt;p&gt;Clearly this is more to flesh out if I use this example…Just food for thought about subclasses.&lt;/p&gt;  &lt;p&gt;As a different example, would it seem natural to have a person entity that was then subclassed into guest and cast member? They do share obvious commonality, and (given the right technology) you would want to track all of their movements. But you probably don’t know that much about guests. Maybe guests aren’t considered people as much as tickets (in the database only) and the ticket is associated with the human characteristics of the guest (height, weight, sex, etc so as to help direct them on their way/predict what they might do? Small kids, fluffy bunny ride?)&amp;#160; Then we might be back to guests and cast members sharing an interface for their location. How alike are location and position?&amp;#160; Probably look alike, but you would want a history of movement if you could track a person’s ever move, right?&lt;/p&gt;  &lt;p&gt;The goal of a database is to store the information in a manner that is useful for analysis both to predict future events, as well as to look at how current processes work each day.&amp;#160; Getting everything just right is the dream of every designer. Of course all of this tells me that I have to be careful with this example or it will grow REALLY huge.&amp;#160; Hopefully I won’t need to finish this too soon, but who knows when the next major version of SQL Server is.&amp;#160; Much to consider, there is.&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=Inheritance in Database Design&amp;amp;body=Seen on SQLblog.com: %0A%0A%09Inheritance in Database Design%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/10/15/inheritance-in-database-design.aspx" target="_blank" title = "Email Inheritance in Database Design"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/10/15/inheritance-in-database-design.aspx&amp;amp;title=Inheritance+in+Database+Design" target="_blank" title = "Submit Inheritance in Database Design to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/10/15/inheritance-in-database-design.aspx&amp;amp;phase=2" target="_blank" title = "Submit Inheritance in Database Design to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/10/15/inheritance-in-database-design.aspx&amp;amp;title=Inheritance+in+Database+Design" target="_blank" title = "Submit Inheritance in Database Design to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/10/15/inheritance-in-database-design.aspx&amp;amp;title=Inheritance+in+Database+Design" target="_blank" title = "Submit Inheritance in Database Design to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/10/15/inheritance-in-database-design.aspx&amp;amp;title=Inheritance+in+Database+Design&amp;amp;;top=1" target="_blank" title = "Add Inheritance in Database Design to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=9516" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>Apologies for comments going unpublished</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/apologies-for-comments-going-unpublished.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/apologies-for-comments-going-unpublished.aspx</id><published>2008-07-31T02:49:51Z</published><updated>2008-07-31T02:49:51Z</updated><content type="html">&lt;p&gt;I had quite a few comments out there that were unpublished.&amp;#160; One person was particularly slighted and I am sending him a &amp;quot;sorry&amp;quot; gift.&amp;#160; I was stuck in &amp;quot;writer rut&amp;quot; and forgot about the fact that comments have to be allowed when people first come to the site (to eliminate spam!)&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=Apologies for comments going unpublished&amp;amp;body=Seen on SQLblog.com: %0A%0A%09Apologies for comments going unpublished%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/apologies-for-comments-going-unpublished.aspx" target="_blank" title = "Email Apologies for comments going unpublished"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/apologies-for-comments-going-unpublished.aspx&amp;amp;title=Apologies+for+comments+going+unpublished" target="_blank" title = "Submit Apologies for comments going unpublished to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/apologies-for-comments-going-unpublished.aspx&amp;amp;phase=2" target="_blank" title = "Submit Apologies for comments going unpublished to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/apologies-for-comments-going-unpublished.aspx&amp;amp;title=Apologies+for+comments+going+unpublished" target="_blank" title = "Submit Apologies for comments going unpublished to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/apologies-for-comments-going-unpublished.aspx&amp;amp;title=Apologies+for+comments+going+unpublished" target="_blank" title = "Submit Apologies for comments going unpublished to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/apologies-for-comments-going-unpublished.aspx&amp;amp;title=Apologies+for+comments+going+unpublished&amp;amp;;top=1" target="_blank" title = "Add Apologies for comments going unpublished to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=8136" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>Commenting your code</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/commenting-your-code.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/commenting-your-code.aspx</id><published>2008-07-31T02:19:43Z</published><updated>2008-07-31T02:19:43Z</updated><content type="html">&lt;p&gt;As I am easing back into real life from writing the book, I am in search of easy targets for blogging.&amp;#160; My boss mentioned &lt;a href="http://www.codinghorror.com/blog/archives/001150.html" target="_blank"&gt;this blog&lt;/a&gt; over on Jeff Atwood's Coding Horror Blog and it got me thinking about commenting.&amp;#160; His advice is to only comment &amp;quot;why&amp;quot; the code works.&amp;#160; I can't quite agree, because the code he claims to be acceptable is:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" size="2"&gt;private double SquareRootApproximation(n) {     &lt;br /&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&amp;#160; r = n / 2;     &lt;br /&gt;&amp;#160; while ( abs( r - (n/r) ) &amp;gt; t ) {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; r = 0.5 * ( r + (n/r) );      &lt;br /&gt;&amp;#160; }      &lt;br /&gt;&amp;#160; return r;      &lt;br /&gt;}      &lt;br /&gt;System.out.println( &amp;quot;r = &amp;quot; + SquareRootApproximation(r) );&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;I mean, it is better than some code I have seen,&amp;#160; but still, I would like a bit more information about why this works.&amp;#160; Maybe the name of the algorithm used, or at least what to do if this fails to provide the expected results.&amp;#160; Admittedly this is probably something that could be easily found, but most algorithms are not.&amp;#160; Comments in my mind should at least lead you to understand the mindset of the programmer.&amp;#160; What would actually improve this code in my mind is to change the variables to full words (though in this case it might not make sense to do this.) &lt;/p&gt;  &lt;p&gt;On an extremely different side of things is &lt;a href="http://www.mssqltips.com/tip.asp?tip=1213" target="_blank"&gt;this article&lt;/a&gt; from &amp;quot;&lt;a href="http://www.mssqltips.com/author.asp?authorid=11"&gt;Edgewood Solutions Engineers&lt;/a&gt;&amp;quot; on mssqltips.com. Their answer is to explain what the code is doing in simple terms, making sure to comment almost everything.&amp;#160; They have a very elaborate header devised, with dependencies, both users of the object and objects it used.&amp;#160; Most of what is said seems a bit like overkill, but their point here &amp;quot;Comment all of the major code blocks of the code and the critical minor points that can be easily overlooked such as a obscure WHERE clause.&amp;quot; is a good one.&amp;#160; I generally pepper my code with comments where I think it will be hard to debug for myself later, with a consideration for others, particularly when those others will call me to explain the code.&lt;/p&gt;  &lt;p&gt;Which brings me to my commenting philosophy. I personally think you have to comment to the expected lowest common denominator.&amp;#160; Think of the dumbest person who could have the need to read your code who is also qualified to have their job (otherwise you would have to write instructions on every line of code). If the qualified person can figure out what you are doing just by your naming conventions and , then it doesn't need comments. But if that person would look at the code and reasonable figure it out, then there is no need to comment the code.&amp;#160; What this requires is a few things:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Naming objects&lt;/strong&gt; - if your procedures, tables, columns, functions all have meaningful names, you won't have to explain what they mean, saving time&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Good design&lt;/strong&gt; - if the relationship between objects and the cardinality of those relationships is clear, then you don't need to explain that what you are doing is hack due to poor thinking...&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Naming variables&lt;/strong&gt; - probably the most important thing to avoid the need for comments is naming stuff.&amp;#160; Name variables with words, not single character values (except sometimes i, x, etc will suffice for obvious typical uses)&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Reasonable code formatting&lt;/strong&gt; - SQL has no real form, so you &lt;em&gt;could&lt;/em&gt; write procedures on a single line.&amp;#160; You could.&amp;#160; You could smash your hand with a hammer too.&amp;#160; Neither action would be very good.&amp;#160; (Consider using Red-Gate's SQL Refactor tool if nothing else.)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;However, the fact is, for SQL code, the real problem comes in when you start coming up with cool relational methods of solving problems that most moderately qualified people wouldn't get. For example the trick of using a sequence table to break apart a comma delimited list. Couple that with a join and you get some amazingly cool code, but how do you comment it?&lt;/p&gt;  &lt;p&gt;For example, say an architect that shouldn't be an architect designs a table with a comma delimited list like this (didn't I mention good design earlier?&amp;#160; I hate having to say this is a hack, but it is an elegant hack...)&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;font color="#008000"&gt;--excerpted from Chapter 7 of &lt;/font&gt;&lt;/em&gt;&lt;a href="http://www.apress.com/book/view/143020866x" target="_blank"&gt;&lt;em&gt;&lt;font color="#008000"&gt;Pro SQL Server 2008 Relational Database Design and Implementation&lt;/font&gt;&lt;/em&gt;&lt;/a&gt;    &lt;br /&gt;&lt;font face="Courier New" size="2"&gt;CREATE TABLE poorDesign      &lt;br /&gt;(      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; poorDesignId int,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; badValue varchar(20)      &lt;br /&gt;)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" size="2"&gt;INSERT INTO poorDesign --using 2008 syntax     &lt;br /&gt;VALUES (1,'1,3,56,7,3,6'),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (2,'22,3'),      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (3,'1')&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" size="2"&gt;&lt;/font&gt;You can &amp;quot;normalize&amp;quot; this set using a table of numbers (in my examples named tools.sequence) and a really cool join:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" size="2"&gt;SELECT&amp;#160;&amp;#160;&amp;#160; poorDesign.poorDesignId as betterDesignId,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SUBSTRING(',' + poorDesign.badValue + ',',i + 1,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CHARINDEX(',',',' + poorDesign.badValue + ',',i + 1) - i - 1)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font face="Courier New" size="2"&gt;as betterScalarValue     &lt;br /&gt;FROM&amp;#160;&amp;#160;&amp;#160;&amp;#160; poorDesign      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; JOIN tools.sequence      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; on i &amp;gt;= 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AND i &amp;lt; LEN(',' + poorDesign.badValue + ',') - 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AND SUBSTRING(',' + + poorDesign.badValue + ',', i, 1) = ','&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;But are there enough pixels available on the planet to make that more understandable to most SQL programmers? Even the reasonably qualified?&amp;#160;&amp;#160; I mean, I am still kind of amazed at the technique and the fact that it returns the following:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" size="2"&gt;betterDesignId betterScalarValue     &lt;br /&gt;-------------- -----------------      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 3      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 56      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 7      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 3      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 6      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 2&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 22      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 2&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 3      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 3&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;still impresses me.&amp;#160; Frankly I don't know how to comment that code to make it readable.&amp;#160; In a real situation I would settle for a comment before the SELECT that stated:&lt;/p&gt;  &lt;p&gt;--Uses a table of numbers to parse the comma delimited list into a SQL acceptable format.   &lt;br /&gt;--If you don't understand this code, read this article: &lt;a title="http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum" href="http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum"&gt;http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Opinions? What do you use for a comments in your code?&amp;#160; Do you have commenting policies?&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=Commenting your code&amp;amp;body=Seen on SQLblog.com: %0A%0A%09Commenting your code%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/commenting-your-code.aspx" target="_blank" title = "Email Commenting your code"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/commenting-your-code.aspx&amp;amp;title=Commenting+your+code" target="_blank" title = "Submit Commenting your code to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/commenting-your-code.aspx&amp;amp;phase=2" target="_blank" title = "Submit Commenting your code to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/commenting-your-code.aspx&amp;amp;title=Commenting+your+code" target="_blank" title = "Submit Commenting your code to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/commenting-your-code.aspx&amp;amp;title=Commenting+your+code" target="_blank" title = "Submit Commenting your code to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/30/commenting-your-code.aspx&amp;amp;title=Commenting+your+code&amp;amp;;top=1" target="_blank" title = "Add Commenting your code to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=8132" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>Triggers...Evil?</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/07/13/triggers-evil.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/07/13/triggers-evil.aspx</id><published>2008-07-13T23:16:53Z</published><updated>2008-07-13T23:16:53Z</updated><content type="html">&lt;p&gt;Say it isn't so. &amp;quot;It isn't so.&amp;quot;&amp;#160; Glenn Berry thinks so in his post &lt;a href="http://glennberrysqlperformance.spaces.live.com/blog/cns!45041418ECCAA960!972.entry" target="_blank"&gt;here&lt;/a&gt;. When I read his post I thought I was going to really get into it with Conor's post &lt;a href="http://www.sqlskills.com/blogs/conor/2008/06/18/TheTroubleWithTriggers.aspx" target="_blank"&gt;here&lt;/a&gt;, called the Trouble with Triggers (a title which I had to post because I like Star Trek too,) but frankly I agree with Conor.&amp;#160; Triggers are great tools, when applied correctly.&amp;#160; When they are used in a bad way (which is quite often, from the code I have seen), well, agree with Glenn I must...&lt;/p&gt;  &lt;p&gt;In my book, I advocate triggers for a few things:&lt;/p&gt;  &lt;p&gt;* &lt;i&gt;Cross-database referential integrity (RI)&lt;/i&gt;: Just basic RI, but SQL Server doesn&amp;#8217;t manage declarative constraints across database boundaries. &lt;/p&gt;  &lt;p&gt;* &lt;i&gt;Intra-table, inter-row constraints&lt;/i&gt;: For example, when you need to see that the sum of a column value over multiple rows is less than some value (possibly in another table). &lt;/p&gt;  &lt;p&gt;* &lt;i&gt;Inter-table constraints&lt;/i&gt;: For example, if a value in one table relies on the value in another. This might also be written as a functions-based CHECK constraint, but it is often more maintainable to use a trigger. &lt;/p&gt;  &lt;p&gt;* &lt;i&gt;Introducing desired side effects to your queries&lt;/i&gt;: For example, cascading inserts, maintaining denormalized data, and so on.&lt;/p&gt;  &lt;p&gt;(and I give examples of each.) but I also am careful to say to be extremely careful with them. When my tech reviewer got to the start of my section on triggers, he quickly said that he didn't like them and they shouldn't be used.&amp;#160; By the end he agreed with my examples.&amp;#160; The problem with triggers is just like the problem with several tools that SQL Server gives you.&amp;#160; For example:&lt;/p&gt;  &lt;p&gt;* &lt;em&gt;Stored procedures:&lt;/em&gt; If used to encapsulate set based SQL calls into precompiled batches...good.&amp;#160; Used as a functional language to do work row by row, often with cursors...not so good.&amp;#160; Used to implement lots of business rules...well, that depends on the business rules but often this is where the real trouble comes (that is a future post)&lt;/p&gt;  &lt;p&gt;* &lt;em&gt;Cursors&lt;/em&gt;: Used to do some repetitive task, usually for some maintenance use...good.&amp;#160; Used in place of set based operations because the programmer cannot write good SQL...baaaddd (said properly should sound like a bleating sheep.)&lt;/p&gt;  &lt;p&gt;* &lt;em&gt;Clustered indexes&lt;/em&gt;: Used to cluster on the right sort of key (like a small monotonically increasing value)...good.&amp;#160; Used to cluster on a random value, like a guid...not so good.&amp;#160; Never used as a search argument of any kind, forcing bookmark lookups constantly....well, yuck.&lt;/p&gt;  &lt;p&gt;So just like you don't hammer in a nail with a wrench, or hammer in a screw with any implement, triggers are not to be used improperly.&amp;#160; The problem is that of education.&amp;#160; There are so many people out there who just do what it is they feel like without regards for what is actually correct.&amp;#160; SQL programmers try to do everything in SQL (I have been guilty of this many times) and functional programmers try to do everything one operation at a time.&amp;#160; Some day, once we all trust the SQLCLR to the extent we ought to, perhaps the exact balance can be achieved.&lt;/p&gt;  &lt;p&gt;And Glenn, I completely agree with your reasons to hate triggers: &amp;quot;I witnessed first hand the consequences of over-use of DML triggers in a large, n-tier client/server application.&amp;quot;&amp;#160; I think the important thing in there is the word &amp;quot;over-use.&amp;quot;&amp;#160; I mean, a person could probably get fat eating only celery and drinking water if they over did that.&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=Triggers...Evil?&amp;amp;body=Seen on SQLblog.com: %0A%0A%09Triggers...Evil?%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/07/13/triggers-evil.aspx" target="_blank" title = "Email Triggers...Evil?"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/13/triggers-evil.aspx&amp;amp;title=Triggers...Evil%3f" target="_blank" title = "Submit Triggers...Evil? to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/13/triggers-evil.aspx&amp;amp;phase=2" target="_blank" title = "Submit Triggers...Evil? to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/13/triggers-evil.aspx&amp;amp;title=Triggers...Evil%3f" target="_blank" title = "Submit Triggers...Evil? to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/13/triggers-evil.aspx&amp;amp;title=Triggers...Evil%3f" target="_blank" title = "Submit Triggers...Evil? to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/07/13/triggers-evil.aspx&amp;amp;title=Triggers...Evil%3f&amp;amp;;top=1" target="_blank" title = "Add Triggers...Evil? to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=7805" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>See you at Tech Ed? Or other places?</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/05/30/see-you-at-tech-ed-or-other-places.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/05/30/see-you-at-tech-ed-or-other-places.aspx</id><published>2008-05-30T19:48:36Z</published><updated>2008-05-30T19:48:36Z</updated><content type="html">&lt;p&gt;Next week is Tech Ed Developers, and I will be there working at the OLTP demo station from:&lt;/p&gt;  &lt;p&gt;Tuesday 11:45 &amp;#8211; 2:45    &lt;br /&gt;Wednesday 2:30 &amp;#8211; 6:00     &lt;br /&gt;Thursday 11:45 &amp;#8211; 2:45     &lt;br /&gt;Friday 11:45 &amp;#8211; 2:45&lt;/p&gt;  &lt;p&gt;All times Eastern Daylight Saving Time and a little bit approximate at this point.&amp;#160; Please stop by and say howdy!&amp;#160; I don't exactly yet know what I will be doing completely, but it will be fun, never the less.&lt;/p&gt;  &lt;p&gt;After that I have a few other speaking engagements coming up:&lt;/p&gt;  &lt;p&gt;Nashville SQL User's Group - Late Summer - Doing some session in concert with my book release (hopefully doing some special stuff that time too.&amp;#160; Stay tuned for that announcement)   &lt;br /&gt;Devlink (&lt;a href="http://www.devlink.net"&gt;www.devlink.net&lt;/a&gt;) - August 22, 23 - Doing a double session on Database Design with Paul Nielsen    &lt;br /&gt;PASS (&lt;a href="http://www.sqlpass.org"&gt;www.sqlpass.org&lt;/a&gt;) - Nov 19-21 - Doing the same session with Paul, just condensed into a single session&lt;/p&gt;  &lt;p&gt;I will be back in the forums soon, and on this blog and my personal website (drsql.org) too as the principal writing for the book will be finished tomorrow, and rewrites in a week or so.&amp;#160; After that, it is a downhill journey to the finish line.&lt;/p&gt;  &lt;p&gt;So come by next week and let me know you are reading the blog, book, or whatever.&amp;#160; &lt;/p&gt;  &lt;p&gt;(cross-posted to drsql.spaces.live.com)&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=See you at Tech Ed? Or other places?&amp;amp;body=Seen on SQLblog.com: %0A%0A%09See you at Tech Ed? Or other places?%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/05/30/see-you-at-tech-ed-or-other-places.aspx" target="_blank" title = "Email See you at Tech Ed? Or other places?"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/05/30/see-you-at-tech-ed-or-other-places.aspx&amp;amp;title=See+you+at+Tech+Ed%3f+Or+other+places%3f" target="_blank" title = "Submit See you at Tech Ed? Or other places? to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/05/30/see-you-at-tech-ed-or-other-places.aspx&amp;amp;phase=2" target="_blank" title = "Submit See you at Tech Ed? Or other places? to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/05/30/see-you-at-tech-ed-or-other-places.aspx&amp;amp;title=See+you+at+Tech+Ed%3f+Or+other+places%3f" target="_blank" title = "Submit See you at Tech Ed? Or other places? to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/05/30/see-you-at-tech-ed-or-other-places.aspx&amp;amp;title=See+you+at+Tech+Ed%3f+Or+other+places%3f" target="_blank" title = "Submit See you at Tech Ed? Or other places? to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/05/30/see-you-at-tech-ed-or-other-places.aspx&amp;amp;title=See+you+at+Tech+Ed%3f+Or+other+places%3f&amp;amp;;top=1" target="_blank" title = "Add See you at Tech Ed? Or other places? to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=7085" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>Sequence Table Tricks</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/03/23/sequence-table-tricks.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/03/23/sequence-table-tricks.aspx</id><published>2008-03-23T14:06:50Z</published><updated>2008-03-23T14:06:50Z</updated><content type="html">&lt;p&gt;Ok, so I am writing about the kinds of things you can do with a sequence table, and I have built the table, I have the typical kinds of things planned (like splitting a string, and for the next section, loading a calendar table) but I wanted to do something interesting.&amp;#160; And frankly, sometimes the path to solving a real problem starts with with solving an abstract problem, then reality often tosses you a problem that is really close to this.&amp;#160; Admittedly this might not be realistic in my case, but it is possible.&lt;/p&gt;  &lt;p&gt;I have just recently watched the Futurama video &amp;quot;Bender's Big Score&amp;quot; and they have a little math lesson section on the video that was very interesting, but the thing that stuck in my mind was this reference back to a previous episode called the &amp;quot;&lt;a href="http://www.things.org/~jym/y3k/2ACV06.html" target="_blank"&gt;Lesser of Two Evils&lt;/a&gt;&amp;quot;.&amp;#160; The Bender look-alike named Flexo (they are both Bender units) start talking and have the following exchange:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Bender: &lt;/b&gt;Hey, brobot, what's your serial number?     &lt;br /&gt;&lt;b&gt;Flexo: &lt;/b&gt;3370318.     &lt;br /&gt;&lt;b&gt;Bender: &lt;/b&gt;No way! Mine's 2716057!     &lt;br /&gt;&lt;b&gt;Fry: &lt;/b&gt;I don't get it.     &lt;br /&gt;&lt;b&gt;Bender: &lt;/b&gt;We're both expressible as the sum of two cubes! &lt;/p&gt;  &lt;p&gt;So I figured, the sum of two cubes would be an interesting, and pretty easy abstract utilization of the sequence table.&amp;#160; Then I have found this reference also to &lt;a href="http://www.sciencenews.org/articles/20020727/mathtrek.asp" target="_blank"&gt;&amp;quot;taxicab&amp;quot; numbers,&lt;/a&gt; where the goal is to discover the smallest value that can be expressed as the sum of three cubes in N different ways.&lt;/p&gt;  &lt;p&gt;How hard is the query? Turns out, that once you have a sequence table with numbers from 1 to 100000 or so, you can calculate that Taxicab(2) = 1729 very easily (and all of the other numbers that are the sum of two cubes too), and the the sum of two cubes in three different ways also pretty easily (took 3 seconds on my laptop, and that value is 87539319).&amp;#160; &lt;/p&gt;  &lt;p&gt;Here is the code:&lt;/p&gt;  &lt;p&gt;declare @level int &lt;/p&gt;  &lt;p&gt;set @level = 2 --sum of two cubes &lt;/p&gt;  &lt;p&gt;;with cubes as    &lt;br /&gt;(select POWER(i,3) as i3     &lt;br /&gt;from&amp;#160;&amp;#160; tools.sequence     &lt;br /&gt;where&amp;#160; i &amp;gt;= 1 and i &amp;lt; 500) --&amp;lt;&amp;lt;&amp;lt;Vary for performance, and for cheating reasons, max needed value     &lt;br /&gt;    &lt;br /&gt;select c1.i3 + c2.i3 as [sum of 2 cubes in N Ways]     &lt;br /&gt;from&amp;#160;&amp;#160; cubes as c1     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cross join cubes as c2     &lt;br /&gt;where c1.i3 &amp;lt;= c2.i3     &lt;br /&gt;group by (c1.i3 + c2.i3)     &lt;br /&gt;having count(*) = @level     &lt;br /&gt;order by 1 &lt;/p&gt;  &lt;p&gt;Ok, breaking this down the cubes CTE is pretty simple: &lt;/p&gt;  &lt;p&gt;(select power(i,3) as i3    &lt;br /&gt;from&amp;#160;&amp;#160; tools.sequence&amp;#160; --the table holds 100000 values     &lt;br /&gt;where&amp;#160; i &amp;gt;= 1 and i &amp;lt; 500) &lt;/p&gt;  &lt;p&gt;This transforms our values to a table of cubes, so the values would be 1, 8, 27, 64, etc.&amp;#160; The query is a bit more interesting. We want the lowest value that meets the criteria that present in a second, so top is used.&amp;#160; I sum the two cube values, which I get from cross joining the CTE twice. &lt;/p&gt;  &lt;p&gt;select c1.i3 + c2.i3 as [sum of 2 cubes in N Ways]    &lt;br /&gt;from&amp;#160;&amp;#160; cubes as c1     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cross join cubes as c2     &lt;br /&gt;where c1.i3 &amp;lt;= c2.i3 --this gets rid of the &amp;quot;duplicate&amp;quot; value pairs &lt;/p&gt;  &lt;p&gt;The where condition of c1.i3 &amp;lt;= c2.i3 gets rid of the &amp;quot;duplicate&amp;quot; value pairs since c1 and c2 have the same values, so without this, for 1729 you would get:&lt;/p&gt;  &lt;p&gt;c1.&lt;font face="Courier New" size="2"&gt;i3&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c2.i3      &lt;br /&gt;-------------------- --------------------       &lt;br /&gt;1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1728       &lt;br /&gt;729&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1000&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" size="2"&gt;1000&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 729      &lt;br /&gt;1728&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;These pairs are the same.&amp;#160; I don't eliminate equality to allow for the case where both number are equal, because they won't be doubled up.&amp;#160; With these values:&lt;/p&gt; c1.&lt;font face="Courier New" size="2"&gt;i3&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c2.i3    &lt;br /&gt;-------------------- --------------------     &lt;br /&gt;1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1728     &lt;br /&gt;729&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1000&lt;/font&gt;   &lt;p&gt;You can see that 1729 is the sum of two cubes in two different ways.&amp;#160; So, lastly, the question of performance must come up.&amp;#160; Reading the articles, it is clear that this is not a terribly easy problem to solve.&amp;#160; Values for the sum of three cubes is fairly simple, leaving the sequence values bounded at 500, I get two values in around one second. &lt;/p&gt;  &lt;p&gt;[sum of 2 cubes in N Ways]    &lt;br /&gt;---------------------------------     &lt;br /&gt;87539319     &lt;br /&gt;119824488 &lt;/p&gt;  &lt;p&gt;Four however, was a &amp;quot;bit&amp;quot; more challenging.&amp;#160; Knowing the answer from the article, I knew I could bound my numbers using 20000 and get the answer.&amp;#160; Using this &amp;quot;cheat&amp;quot; on my laptop, I was able to calculate the value of taxicab(4) was 6963472309248 (yea, it only found one) in just 1 hour and 33 minutes, this on a 2.2 Ghz Pentium M laptop with 2 GB of RAM. I tried calculating taxicab(5), but alas, I ran out of space for tempdb (and I had 50 GB available.)&amp;#160; For that you had to go up to i being greater than something like 350000....&lt;/p&gt;  &lt;p&gt;I am thinking that if I turn the CTE into a physical, indexed table I would be able to do this.&amp;#160; And for 6, perhaps using the precision of the numeric datatype (38 places!), but not today, I don't want to melt my computer trying something this abstract.&amp;#160; Well at least not just yet, perhaps when the &lt;a href="http://www.apress.com/book/view/143020866x" target="_blank"&gt;book&lt;/a&gt; is done...&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=Sequence Table Tricks&amp;amp;body=Seen on SQLblog.com: %0A%0A%09Sequence Table Tricks%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/03/23/sequence-table-tricks.aspx" target="_blank" title = "Email Sequence Table Tricks"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/23/sequence-table-tricks.aspx&amp;amp;title=Sequence+Table+Tricks" target="_blank" title = "Submit Sequence Table Tricks to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/23/sequence-table-tricks.aspx&amp;amp;phase=2" target="_blank" title = "Submit Sequence Table Tricks to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/23/sequence-table-tricks.aspx&amp;amp;title=Sequence+Table+Tricks" target="_blank" title = "Submit Sequence Table Tricks to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/23/sequence-table-tricks.aspx&amp;amp;title=Sequence+Table+Tricks" target="_blank" title = "Submit Sequence Table Tricks to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/23/sequence-table-tricks.aspx&amp;amp;title=Sequence+Table+Tricks&amp;amp;;top=1" target="_blank" title = "Add Sequence Table Tricks to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=5748" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>2008: Error List in 2008</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/03/05/2008-error-list-in-2008.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/03/05/2008-error-list-in-2008.aspx</id><published>2008-03-06T03:40:47Z</published><updated>2008-03-06T03:40:47Z</updated><content type="html">&lt;p&gt;Ok, so I was looking around for a blurb about plan guides for my anti ad hoc SQL section of my book (ok, maybe not completely anti- but that isn't the point,) when I found this blog: &lt;a title="http://geekswithblogs.net/Sreeblog/articles/117576.aspx" href="http://geekswithblogs.net/Sreeblog/articles/117576.aspx"&gt;http://geekswithblogs.net/Sreeblog/articles/117576.aspx&lt;/a&gt; that basically just lists some new features in 2008.&amp;nbsp; One that I hadn't seen I feel the need to mention.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Transact-SQL Error List Window: &lt;br&gt;SQL Server Management Studio includes an Error List window that displays the syntax and semantic errors generated from the IntelliSense code in the Transact-SQL Query Editor.  &lt;p&gt;If you haven't seen this, it is really kind of neat.&amp;nbsp; Say you type:  &lt;p&gt;select *&lt;br&gt;from sys.object&lt;br&gt;whe re name = 'fred'  &lt;p&gt;You can see the obvious errors, since this is a very small batch, but IntelliSense underlines sys.object and re in the batch.&amp;nbsp; Go to the View menu and show the Error List, and you will see something like this:  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/louis_davidson/WindowsLiveWriter/2008ErrorListin2008_130DA/image_2.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="321" alt="image" src="http://sqlblog.com/blogs/louis_davidson/WindowsLiveWriter/2008ErrorListin2008_130DA/image_thumb.png" width="582" border="0"&gt;&lt;/a&gt;&amp;nbsp; &lt;p&gt;Double-click on the error, it takes you to the error in the query window.&amp;nbsp; Obviously I have just discovered this, so please comment if you know more about this topic (particularly if it has been helpful to you or not) please chime in.&amp;nbsp; And thanks to Sreenivas Mogullapalli for the cool post, it had a few other things I didn't remember too (just don't make me have to pronounce your name...I am pretty sure I wouldn't get it right.)  
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=2008: Error List in 2008&amp;amp;body=Seen on SQLblog.com: %0A%0A%092008: Error List in 2008%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/03/05/2008-error-list-in-2008.aspx" target="_blank" title = "Email 2008: Error List in 2008"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/05/2008-error-list-in-2008.aspx&amp;amp;title=2008%3a+Error+List+in+2008" target="_blank" title = "Submit 2008: Error List in 2008 to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/05/2008-error-list-in-2008.aspx&amp;amp;phase=2" target="_blank" title = "Submit 2008: Error List in 2008 to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/05/2008-error-list-in-2008.aspx&amp;amp;title=2008%3a+Error+List+in+2008" target="_blank" title = "Submit 2008: Error List in 2008 to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/05/2008-error-list-in-2008.aspx&amp;amp;title=2008%3a+Error+List+in+2008" target="_blank" title = "Submit 2008: Error List in 2008 to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/05/2008-error-list-in-2008.aspx&amp;amp;title=2008%3a+Error+List+in+2008&amp;amp;;top=1" target="_blank" title = "Add 2008: Error List in 2008 to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=5474" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author><category term="SQL Server" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/SQL+Server/default.aspx" /><category term="SQL Tools" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/SQL+Tools/default.aspx" /><category term="SSMS" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/SSMS/default.aspx" /></entry><entry><title>2008: Declaring and instantiating a value</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/03/01/2008-declaring-and-instantiating-a-value.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/03/01/2008-declaring-and-instantiating-a-value.aspx</id><published>2008-03-02T00:40:00Z</published><updated>2008-03-02T00:40:00Z</updated><content type="html">&lt;P&gt;Ok, I admit it.&amp;nbsp; Sometimes the least important things are the most fun.&amp;nbsp; As I try to get my blog back up and kicking again after a few months of holiday fun coupled with some dreary personal life things (a death in the family and lots of sickness/busyness, mostly,) I felt the need to write about another little time saving feature that you might not have heard of. Ever if you have it is still cool.&lt;/P&gt;
&lt;P&gt;This topic is declaring and instantiating a value in a single statement.&amp;nbsp; So what used to be:&lt;/P&gt;
&lt;P&gt;DECLARE @i int&lt;BR&gt;SET @i = 1&lt;/P&gt;
&lt;P&gt;Can now be:&lt;/P&gt;
&lt;P&gt;DECLARE @i int = 1&lt;/P&gt;
&lt;P&gt;Ho hum, I had thought earlier when I first saw this.&amp;nbsp; You know, it saves me 3 keystrokes. But today, I was doing some writing in my book and I realized that it isn't just limited to literals (it had just never crossed my mind) so when I was creating my savepoint names for nested savepoints.&amp;nbsp; You can use literals and functions, whatever you need.&lt;/P&gt;
&lt;P&gt;DECLARE @savepoint nvarchar(128) = cast(object_name(@@procid) AS nvarchar(125)) + cast(@@nestlevel AS nvarchar(3)) 
&lt;P&gt;All in one line of code...clean, neat.&amp;nbsp; And when I was writing a trigger that I could do this: 
&lt;P&gt;DECLARE @rowsAffected int = @@rowcount, --stores the number of rows affected&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @msg varchar(2000) = '' --used to hold the error message 
&lt;P&gt;And save a couple of lines of code, and now, how much cleaner and effective is this code?&amp;nbsp; Not only does it save two lines of code to do the SET (or you could use SELECT), but it saves lines of whitespace too.&amp;nbsp; 
&lt;P&gt;Will this save a lot of time?&amp;nbsp; Probably not, but it is one of those long desired features that we SQL Server programmers are so glad to be getting.&amp;nbsp; In my case it is going to cost me time because I have to go back and re-edit places where I forgot about this syntax and didn't use it in my new book, which is one of the worst parts of writing.&amp;nbsp; When you mess up and forget something that is new and more or less essential, you can have to edit large amounts of code/text. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;The comment from Steve got me thinking if you could use&amp;nbsp;a query to instatiate the value.&amp;nbsp;&amp;nbsp; Survey says:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;create table fred&lt;BR&gt;(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value&amp;nbsp;&amp;nbsp; char(1)&lt;BR&gt;)&lt;BR&gt;go&lt;BR&gt;insert into fred&lt;BR&gt;values ('a'),('b'),('c')&lt;BR&gt;go&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;declare @value char(1) = (select MAX(value) from fred)&lt;/P&gt;
&lt;P&gt;select @value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;YES! This returns 'c', just as you would expect.&amp;nbsp; Okay, so now this is even cooler.&amp;nbsp; It doesn't work with table types though. And since we are feature complete, I assume it probably won't (I didn't expect it too, but it never hurts to try.)&lt;FONT size=2&gt;&lt;/P&gt;&lt;/FONT&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=2008: Declaring and instantiating a value&amp;amp;body=Seen on SQLblog.com: %0A%0A%092008: Declaring and instantiating a value%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/03/01/2008-declaring-and-instantiating-a-value.aspx" target="_blank" title = "Email 2008: Declaring and instantiating a value"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/01/2008-declaring-and-instantiating-a-value.aspx&amp;amp;title=2008%3a+Declaring+and+instantiating+a+value" target="_blank" title = "Submit 2008: Declaring and instantiating a value to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/01/2008-declaring-and-instantiating-a-value.aspx&amp;amp;phase=2" target="_blank" title = "Submit 2008: Declaring and instantiating a value to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/01/2008-declaring-and-instantiating-a-value.aspx&amp;amp;title=2008%3a+Declaring+and+instantiating+a+value" target="_blank" title = "Submit 2008: Declaring and instantiating a value to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/01/2008-declaring-and-instantiating-a-value.aspx&amp;amp;title=2008%3a+Declaring+and+instantiating+a+value" target="_blank" title = "Submit 2008: Declaring and instantiating a value to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/03/01/2008-declaring-and-instantiating-a-value.aspx&amp;amp;title=2008%3a+Declaring+and+instantiating+a+value&amp;amp;;top=1" target="_blank" title = "Add 2008: Declaring and instantiating a value to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=5390" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author><category term="SQL Server" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/SQL+Server/default.aspx" /><category term="T-SQL" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/T-SQL/default.aspx" /></entry><entry><title>2008: Rebuilding a Heap</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2008/02/26/2008-rebuilding-a-heap.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2008/02/26/2008-rebuilding-a-heap.aspx</id><published>2008-02-27T04:10:24Z</published><updated>2008-02-27T04:10:24Z</updated><content type="html">&lt;p&gt;In 2005, rebuilding a table that was a heap (no clustered index) wasn't easy.&amp;nbsp; You could copy it to a different table, or you could add a clustered index and then drop it. &lt;/p&gt; &lt;p&gt;In 2008, this is a far easier thing to do.&amp;nbsp; They have added to the ALTER TABLE command a method to rebuild the table, which is the same as rebuilding the clustered index for a clustered table, but for a HEAP, it is the only way to go.&amp;nbsp; &lt;/p&gt; &lt;p&gt;In the following code sample, I create a heap, load it will "little data", then expand every row to much larger values.&amp;nbsp; The result is a little bit of fragmentation, but more than that, tons of forwarding pointers (when a row won't fit on the same heap page, it gets moved to a different page, but the pointer to the row does not change in the indexes.)&amp;nbsp; Rebuilding the heap is now really simple:&lt;/p&gt; &lt;p&gt;&lt;font color="#004080"&gt;create table heapDemo&lt;br&gt;(&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value varchar(1000)&lt;br&gt;)&lt;br&gt;GO&lt;br&gt;set nocount on&lt;br&gt;insert into heapDemo&lt;br&gt;select 'hi'&lt;br&gt;go 10000&lt;br&gt;--Expand the values to 500 times the size they were&lt;br&gt;update heapDemo&lt;br&gt;set value = replicate('hi',500)&lt;/font&gt;&lt;/p&gt; &lt;p&gt;Now, check the stats of the table (using the index stats dmv, no less)&lt;/p&gt; &lt;p&gt;&lt;font color="#004080"&gt;select index_type_desc, fragment_count, page_count, forwarded_record_count&amp;nbsp; &lt;br&gt;from sys.dm_db_index_physical_stats(db_id(),default,default,default,'DETAILED')&lt;br&gt;where object_id = object_id('heapDemo')&lt;/font&gt; &lt;/p&gt; &lt;p&gt;This returns:&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New" size="1"&gt;index_type_desc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fragment_count&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; page_count&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; forwarded_record_count&lt;br&gt;--------------------- -------------------- -------------------- ----------------------&lt;br&gt;HEAP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1443&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9961&lt;/font&gt;&lt;br&gt;&lt;/p&gt; &lt;p&gt;Now, you can rebuild the heap with the command:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;font color="#004080"&gt;alter table heapDemo rebuild&lt;/font&gt;&lt;/strong&gt;&lt;br&gt;&lt;/p&gt; &lt;p&gt;Check the values now:&lt;/p&gt; &lt;p&gt;&lt;font color="#004080"&gt;select index_type_desc, fragment_count, page_count, forwarded_record_count&lt;br&gt;from sys.dm_db_index_physical_stats(db_id(),default,default,default,'DETAILED')&lt;br&gt;where object_id = object_id('heapDemo')&lt;/font&gt; &lt;/p&gt; &lt;p&gt;This shows no that the forwarded_record_count is 0, which is the idea situation for your heap.&lt;/p&gt;&lt;font face="Courier New" size="1"&gt;index_type_desc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fragment_count&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; page_count&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; forwarded_record_count&lt;br&gt;--------------------- -------------------- -------------------- ----------------------&lt;br&gt;HEAP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1440&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/font&gt;&lt;br&gt; &lt;p&gt;Nice new addition!&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=2008: Rebuilding a Heap&amp;amp;body=Seen on SQLblog.com: %0A%0A%092008: Rebuilding a Heap%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2008/02/26/2008-rebuilding-a-heap.aspx" target="_blank" title = "Email 2008: Rebuilding a Heap"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/02/26/2008-rebuilding-a-heap.aspx&amp;amp;title=2008%3a+Rebuilding+a+Heap" target="_blank" title = "Submit 2008: Rebuilding a Heap to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/02/26/2008-rebuilding-a-heap.aspx&amp;amp;phase=2" target="_blank" title = "Submit 2008: Rebuilding a Heap to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/02/26/2008-rebuilding-a-heap.aspx&amp;amp;title=2008%3a+Rebuilding+a+Heap" target="_blank" title = "Submit 2008: Rebuilding a Heap to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2008/02/26/2008-rebuilding-a-heap.aspx&amp;amp;title=2008%3a+Rebuilding+a+Heap" target="_blank" title = "Submit 2008: Rebuilding a Heap to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2008/02/26/2008-rebuilding-a-heap.aspx&amp;amp;title=2008%3a+Rebuilding+a+Heap&amp;amp;;top=1" target="_blank" title = "Add 2008: Rebuilding a Heap to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=5295" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author><category term="SQL Server" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/SQL+Server/default.aspx" /><category term="T-SQL" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/T-SQL/default.aspx" /><category term="Dynamic Management Objects" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/Dynamic+Management+Objects/default.aspx" /></entry><entry><title>2008: Initializing Table Data with Row Constructors</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/2008-initializing-table-data-with-row-constructors.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/2008-initializing-table-data-with-row-constructors.aspx</id><published>2007-12-06T01:50:44Z</published><updated>2007-12-06T01:50:44Z</updated><content type="html">&lt;p&gt;Well, I am just discovering this feature, mostly because I never saw it demoed at any of the sessions I have attended so far on 2008.&amp;nbsp; Not that it was kept particularly hidden, I have seen the title before, but I hadn't tried it out, or seen the depth that they have "finally" implemented.&lt;/p&gt; &lt;p&gt;Tonight, I am working on my chapter where I create some tables as part of a big example, and I had the code from the 2005 version of the book (and I add the primary key to the table later in the book, as well as other constraints, so don't judge me!):&lt;/p&gt; &lt;p&gt;CREATE TABLE Inventory.MovieRating (&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MovieRatingId&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int NOT NULL,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Code&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; varchar(20) NOT NULL,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Description&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; varchar(200) NULL,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AllowYouthRentalFlag bit NOT NULL&lt;br&gt;)&lt;br&gt;GO&lt;/p&gt; &lt;p&gt;INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)&lt;br&gt;VALUES (0, 'UR','Unrated',1)&lt;br&gt;INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)&lt;br&gt;VALUE&amp;nbsp;&amp;nbsp; (1, 'G','General Audiences',1),&lt;br&gt;INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)&lt;br&gt;VALUES (2, 'PG','Parental Guidance',1),&lt;br&gt;INSERT INTO Inventory.MovieRating(MovieRatingId, Code, Description, AllowYouthRentalFlag)&lt;br&gt;VALUES (3, 'PG-13','Parental Guidance for Children Under 13',1),&lt;br&gt;INSERT INTO Inventory.MovieRating(MovieRatingId, Code, Description, AllowYouthRentalFlag)&lt;br&gt;VALUES (4, 'R','Restricted, No Children Under 17 without Parent',0)&lt;/p&gt; &lt;p&gt;(Another variety is to use:&lt;/p&gt; &lt;p&gt;INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)&lt;br&gt;SELECT 0, 'UR','Unrated',1&lt;br&gt;UNION ALL&lt;br&gt;SELECT 1, 'G','General Audiences',1&lt;br&gt;UNION ALL&lt;br&gt;SELECT 3, 'PG-13','Parental Guidance for Children Under 13',1&lt;br&gt;UNION ALL&lt;br&gt;SELECT 4, 'R','Restricted, No Children Under 17 without Parent',0&lt;/p&gt; &lt;p&gt;But that is not that much better (certainly a little better).&amp;nbsp; I felt for the book that using VALUES was the more "proper" way to do it.&amp;nbsp; However, now, in the 2008 edition, I obviously have to change &lt;em&gt;all &lt;/em&gt;of the code to use the latest and greatest syntax, so I use row constructors, and this turns into:&lt;/p&gt; &lt;p&gt;INSERT INTO Inventory.MovieRating (MovieRatingId, Code, Description, AllowYouthRentalFlag)&lt;br&gt;VALUES (0, 'UR','Unrated',1),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (1, 'G','General Audiences',1),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (2, 'PG','Parental Guidance',1),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (3, 'PG-13','Parental Guidance for Children Under 13',1),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (4, 'R','Restricted, No Children Under 17 without Parent',0)&lt;/p&gt; &lt;p&gt;And it just strikes me at how...simple this is, and how readable this is.&amp;nbsp; I know I have typed the UNION ALL stuff hundreds, possibly thousands (well hundreds more like) of times in the forums over the past year and a half dummying up data for someone who was asking for help but couldn't take the time to do it for us.&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=2008: Initializing Table Data with Row Constructors&amp;amp;body=Seen on SQLblog.com: %0A%0A%092008: Initializing Table Data with Row Constructors%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/2008-initializing-table-data-with-row-constructors.aspx" target="_blank" title = "Email 2008: Initializing Table Data with Row Constructors"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/2008-initializing-table-data-with-row-constructors.aspx&amp;amp;title=2008%3a+Initializing+Table+Data+with+Row+Constructors" target="_blank" title = "Submit 2008: Initializing Table Data with Row Constructors to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/2008-initializing-table-data-with-row-constructors.aspx&amp;amp;phase=2" target="_blank" title = "Submit 2008: Initializing Table Data with Row Constructors to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/2008-initializing-table-data-with-row-constructors.aspx&amp;amp;title=2008%3a+Initializing+Table+Data+with+Row+Constructors" target="_blank" title = "Submit 2008: Initializing Table Data with Row Constructors to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/2008-initializing-table-data-with-row-constructors.aspx&amp;amp;title=2008%3a+Initializing+Table+Data+with+Row+Constructors" target="_blank" title = "Submit 2008: Initializing Table Data with Row Constructors to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/2008-initializing-table-data-with-row-constructors.aspx&amp;amp;title=2008%3a+Initializing+Table+Data+with+Row+Constructors&amp;amp;;top=1" target="_blank" title = "Add 2008: Initializing Table Data with Row Constructors to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=3724" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry><entry><title>Changing the owner of a database</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/changing-the-owner-of-a-database.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/changing-the-owner-of-a-database.aspx</id><published>2007-12-05T05:06:46Z</published><updated>2007-12-05T05:06:46Z</updated><content type="html">&lt;p&gt;Tonight, as I was creating my sample database for my chapter on implementing the database, I learned something new, that existed in 2005.&amp;nbsp; I had always used sp_changedbowner to change the owner of a database, but I was reading in another section about ALTER AUTHORIZATION earlier to change the owner of an object in a schema, so I said to my self "Louis" (I call myself that) "Louis, wonder if ALTER AUTHORIZATION works on other stuff?"&lt;/p&gt; &lt;p&gt;Bizarre and surreal conversations with myself aside, the answer was Yes, you sure could.&amp;nbsp; The syntax is:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;ALTER AUTHORIZATION ON Database::&amp;lt;databaseName&amp;gt; TO &amp;lt;serverPrincipal&amp;gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;To demonstrate, first, I will create a login:&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New"&gt;create login test with password = 'like, password, dude'&lt;br&gt;go&lt;/font&gt;&lt;/p&gt; &lt;p&gt;Then a database just taking all of the defaults.&amp;nbsp; Setting an owner is not a part of the CREATE DATABASE syntax anyhow:&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New"&gt;create database showChangeOwner&lt;br&gt;go&lt;/font&gt;&lt;/p&gt; &lt;p&gt;Then, check the owner:&lt;/p&gt; &lt;p&gt;&lt;font face="Courier New"&gt;select SUSER_SNAME(owner_sid)&lt;br&gt;from&amp;nbsp;&amp;nbsp; sys.databases&lt;br&gt;where&amp;nbsp; name = 'showChangeOwner'&lt;/font&gt; &lt;/p&gt; &lt;p&gt;This will return the login that you resolve to when you created the database.&amp;nbsp; &lt;p&gt;&lt;font face="Courier New"&gt;MYDOMAIN\LBDAVI&lt;/font&gt;  &lt;p&gt;Next, run the following statement to change the owner: &lt;p&gt;&lt;strong&gt;ALTER AUTHORIZATION ON Database::showChangeOwner TO test&lt;/strong&gt;  &lt;p&gt;Then run the previous statement to see that the database is now owned by login: Test . &lt;p&gt;As an aside, if you use EXECUTE AS to change context to a different login, any database you create will be created as the user you are EXECUTING AS, so this could technically be the way to set the owner at creation time.&amp;nbsp; I will give the test login rights to create a database then execute as it. (My new favorite permission is "create any database".&amp;nbsp; I looked, but despite the connotation, there is not a "create almost any database", "create just this one database", or even "create database named fred" rights.  &lt;p&gt;&lt;font face="Courier New"&gt;grant create any database to test&lt;br&gt;execute as login = 'test'&lt;br&gt;create database showChangeOwner2 &lt;/font&gt; &lt;p&gt;Checking the owner again:  &lt;p&gt;select SUSER_SNAME(owner_sid)&lt;br&gt;from&amp;nbsp;&amp;nbsp; sys.databases&lt;br&gt;where&amp;nbsp; name = 'showChangeOwner2'  &lt;p&gt;Will show that this is owned by user "test". ALTER AUTHORIZATION will actually let you change the owner of quite a few different things.&amp;nbsp; For more information, please visit your public library...wait, no this is 2007, not 1970...&amp;nbsp; Check &lt;a href="http://msdn2.microsoft.com/en-us/library/ms187359.aspx" target="_blank"&gt;here&lt;/a&gt; in books online.&lt;/p&gt; &lt;p&gt;Don't forget to clean up!&lt;/p&gt; &lt;p&gt;drop database showChangeOwner, showChangeOwner2&lt;/p&gt; &lt;p&gt;&lt;em&gt;(did you know you could do that?&amp;nbsp; Saved me having to type drop database two times.&amp;nbsp; Wow, the time I saved by not typing drop database.&amp;nbsp; Wait, I typed drop database even more times after that.&amp;nbsp; And yes, this obvious lack of material is why I haven't been blogging of late...)&lt;/em&gt;&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=Changing the owner of a database&amp;amp;body=Seen on SQLblog.com: %0A%0A%09Changing the owner of a database%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/changing-the-owner-of-a-database.aspx" target="_blank" title = "Email Changing the owner of a database"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/changing-the-owner-of-a-database.aspx&amp;amp;title=Changing+the+owner+of+a+database" target="_blank" title = "Submit Changing the owner of a database to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/changing-the-owner-of-a-database.aspx&amp;amp;phase=2" target="_blank" title = "Submit Changing the owner of a database to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/changing-the-owner-of-a-database.aspx&amp;amp;title=Changing+the+owner+of+a+database" target="_blank" title = "Submit Changing the owner of a database to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/changing-the-owner-of-a-database.aspx&amp;amp;title=Changing+the+owner+of+a+database" target="_blank" title = "Submit Changing the owner of a database to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2007/12/05/changing-the-owner-of-a-database.aspx&amp;amp;title=Changing+the+owner+of+a+database&amp;amp;;top=1" target="_blank" title = "Add Changing the owner of a database to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=3690" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author><category term="SQL Server" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/SQL+Server/default.aspx" /><category term="T-SQL" scheme="http://sqlblog.com/blogs/louis_davidson/archive/tags/T-SQL/default.aspx" /></entry><entry><title>sys.dm_exec_xml_handles</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/louis_davidson/archive/2007/11/04/sys-dm-exec-xml-handles.aspx" /><id>http://sqlblog.com/blogs/louis_davidson/archive/2007/11/04/sys-dm-exec-xml-handles.aspx</id><published>2007-11-04T20:53:59Z</published><updated>2007-11-04T20:53:59Z</updated><content type="html">&lt;p&gt;d Gives you information about any sessions that are using xml documents, including stats about the XML document and usage.&amp;nbsp; You can also get the statements that were used to create the XML handle using sys.dm_exec_sql_text to get the sql.  &lt;p&gt;&lt;b&gt;Type:&lt;/b&gt; Function&lt;br&gt;&lt;b&gt;Parameter: &lt;/b&gt;session_id&lt;br&gt;&lt;b&gt;Data:&lt;/b&gt; Snapshot, values based on current reality&lt;br&gt;&lt;b&gt;Columns:&lt;/b&gt;  &lt;ul&gt; &lt;li&gt;&lt;b&gt;session_id&lt;/b&gt; - the session_id of the user who is using the sp_xml_preparedocument command to use the XML datatype  &lt;li&gt;&lt;b&gt;document_id&lt;/b&gt; – handle created for the document  &lt;li&gt;&lt;b&gt;namespace_document_id&lt;/b&gt; – you can use sp_xml_preparedocument to declare a document that is&amp;nbsp; namespace, then use it for subsequent calls. This value will have the value of a document_id, or a NULL if there is not one declared  &lt;li&gt;&lt;b&gt;sql_handle&lt;/b&gt; – relates to sys.dm_exec_sql_text to get the T-SQL of the query  &lt;li&gt;&lt;b&gt;statement_start_offset&lt;/b&gt; - the starting point in the T-SQL query object that is currently executing (note that these values are double the expected values due to unicode values. You will have to divide by 2 when using them in equations. For more information, see examples)  &lt;li&gt;&lt;b&gt;statement_end_offset&lt;/b&gt; - the ending point in the T-SQL query object that is currently executing  &lt;li&gt;&lt;b&gt;creation_time&lt;/b&gt; - the time the handle was created  &lt;li&gt;&lt;b&gt;original_document_size_bytes&lt;/b&gt; – size of the original text of the XML document  &lt;li&gt;&lt;b&gt;original_namespace_document_size_bytes - &lt;/b&gt;size of the original text of the XML namespace document&lt;b&gt;&lt;/b&gt;  &lt;li&gt;&lt;b&gt;num_openxml_calls&lt;/b&gt; - Number of times the document has been used in OPENXML calls using this handle  &lt;li&gt;&lt;b&gt;row_count&lt;/b&gt; - Number of rows that have been returned using this handle in OPENXML calls  &lt;li&gt;&lt;b&gt;dormant_duration_ms&lt;/b&gt; - the amount of time (in milliseconds) since the last use of the handle in an OPENXML call&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt;  &lt;p&gt;DECLARE @idoc int&lt;br&gt;DECLARE @doc varchar(1000)&lt;br&gt;&lt;br&gt;SET @doc ='&lt;br&gt;&amp;lt;root&amp;gt;&lt;br&gt;&amp;lt;person firstName="barney" lastName="rubble"/&amp;gt;&lt;br&gt;&amp;lt;person firstName="fred" lastName="flintstone"/&amp;gt;&lt;br&gt;&amp;lt;/root&amp;gt;'&lt;br&gt;&lt;br&gt;--Create an internal representation of the XML document.&lt;br&gt;EXEC sp_xml_preparedocument @idoc OUTPUT, @doc &lt;/p&gt; &lt;p&gt;-- Execute a SELECT statement that uses the OPENXML rowset provider. &lt;br&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp; *&lt;br&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OPENXML (@idoc, '/root/person',1)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WITH (firstName varchar(20),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lastName varchar(20))  &lt;p&gt;Then you can see the information for this document using:  &lt;p&gt;select *, text, substring(text,statement_start_offset /2,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1+ statement_end_offset/2 - statement_start_offset/2),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; statement_start_offset,statement_end_offset&lt;br&gt;from&amp;nbsp;&amp;nbsp;&lt;strong&gt; sys.dm_exec_xml_handles(null)&lt;/strong&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cross apply sys.dm_exec_sql_text(sql_handle)&lt;/p&gt; &lt;p&gt;&lt;em&gt;Note:&amp;nbsp; This is part of an ongoing project to write a book about all of the dynamic management views for &lt;/em&gt;&lt;a href="http://www.red-gate.com/"&gt;&lt;em&gt;Red-Gate&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&amp;nbsp;It will be freely distributable once complete as an ebook.&amp;nbsp;Any examples, suggestions, corrections are desired and will certainly be mentioned in the final book.&amp;nbsp; Also, I have begun maintaining the following web page once the book is closer to completion: &lt;/em&gt;&lt;a href="http://drsql.org/dmvbook.aspx"&gt;&lt;em&gt;http://drsql.org/dmvbook.aspx&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, including a list of resources I have used to write the book so far.&lt;/em&gt;&lt;/p&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=sys.dm_exec_xml_handles&amp;amp;body=Seen on SQLblog.com: %0A%0A%09sys.dm_exec_xml_handles%0A%0Ahttp://sqlblog.com/blogs/louis_davidson/archive/2007/11/04/sys-dm-exec-xml-handles.aspx" target="_blank" title = "Email sys.dm_exec_xml_handles"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/11/04/sys-dm-exec-xml-handles.aspx&amp;amp;title=sys.dm_exec_xml_handles" target="_blank" title = "Submit sys.dm_exec_xml_handles to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/11/04/sys-dm-exec-xml-handles.aspx&amp;amp;phase=2" target="_blank" title = "Submit sys.dm_exec_xml_handles to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/11/04/sys-dm-exec-xml-handles.aspx&amp;amp;title=sys.dm_exec_xml_handles" target="_blank" title = "Submit sys.dm_exec_xml_handles to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/louis_davidson/archive/2007/11/04/sys-dm-exec-xml-handles.aspx&amp;amp;title=sys.dm_exec_xml_handles" target="_blank" title = "Submit sys.dm_exec_xml_handles to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/louis_davidson/archive/2007/11/04/sys-dm-exec-xml-handles.aspx&amp;amp;title=sys.dm_exec_xml_handles&amp;amp;;top=1" target="_blank" title = "Add sys.dm_exec_xml_handles to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=3211" width="1" height="1"&gt;</content><author><name>drsql</name><uri>http://sqlblog.com/members/drsql.aspx</uri></author></entry></feed>