<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'Fun'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Fun&amp;orTags=0</link><description>Search results matching tag 'Fun'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Trigger Authoring Decision Tree</title><link>http://sqlblog.com/blogs/merrill_aldrich/archive/2013/03/06/trigger-authoring-decision-tree.aspx</link><pubDate>Wed, 06 Mar 2013 18:46:09 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:48072</guid><dc:creator>merrillaldrich</dc:creator><description>&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/merrill_aldrich/TriggersDecisionTree_5D8ECB6C.png"&gt;&lt;img title="TriggersDecisionTree" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="TriggersDecisionTree" src="http://sqlblog.com/blogs/merrill_aldrich/TriggersDecisionTree_thumb_03F0AEB8.png" width="666" height="707" /&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>PASS Summit 2011 - What you can expect at the Conference - Summary</title><link>http://sqlblog.com/blogs/rick_heiges/archive/2011/09/19/pass-summit-2011-what-you-can-expect-at-the-conference-summary.aspx</link><pubDate>Mon, 19 Sep 2011 13:30:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:38504</guid><dc:creator>RickHeiges</dc:creator><description>&lt;P&gt;As I finish up preparing for the Sep 20 (@1pm ET) webcast, I thought a final blog post may be appropriate in order to quickly reference many of the areas that I have touched upon.&amp;nbsp; Naturally, much of this information is geared towards First Timers, but Alumni are welcome to join in the webcast as well and are encouraged to help spread even more tips/tricks around to all who attend.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/09/pass-summit-2011-what-you-can-expect-at-the-conference-part-1.aspx" target=_blank&gt;General Overview&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/12/what.aspx" target=_blank&gt;Planning Your Learning Itinerary&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/14/pass-summit-2011-what-you-can-expect-at-the-conference-part-2.aspx" target=_blank&gt;Meals&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/15/pass-summit-2011-what-you-can-expect-at-the-conference-part-4.aspx" target=_blank&gt;More Than Just Sessions&lt;/A&gt;&amp;nbsp; - &lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/20/be-prepared-to-ask-the-css-and-sqlcat-folks-your-toughest-questions.aspx" target=_blank&gt;And Be Prepared&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/16/pass-summit-2011-what-you-can-expect-at-the-conference-part-5.aspx" target=_blank&gt;Networking&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/16/pass-summit-2011-what-you-can-expect-at-the-conference-part-6.aspx" target=_blank&gt;After Hours&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/16/pass-summit-2011-what-you-can-expect-at-the-conference-part-7.aspx" target=_blank&gt;Social Networking Tools&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlblog.com/blogs/rick_heiges/archive/2011/09/19/pass-summit-2011-what-you-can-expect-at-the-conference-part-8.aspx" target=_blank&gt;Logistics&lt;/A&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Register &lt;A href="https://www.livemeeting.com/lrs/8000181573/Registration.aspx?pageName=79tznhrs771f4tdn" target=_blank&gt;HERE &lt;/A&gt;for the webcast.&amp;nbsp; It will be recorded and be available for replay shortly thereafter.&lt;/P&gt;</description></item><item><title>PASS Summit 2011 - What you can expect at the Conference - Part 7</title><link>http://sqlblog.com/blogs/rick_heiges/archive/2011/09/16/pass-summit-2011-what-you-can-expect-at-the-conference-part-7.aspx</link><pubDate>Fri, 16 Sep 2011 16:30:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:38473</guid><dc:creator>RickHeiges</dc:creator><description>&lt;P&gt;I really don't think that you can get the full flavor of the Summit unless you delve into Social Networking.&amp;nbsp; There are multiple vehicles for social networking.&amp;nbsp; Twitter, Facebook, Foursquare, LinkedIn, and Yelp are all tools in the Social Networking Toolbox.&amp;nbsp; At the Summit, the best one of these is Twitter.&amp;nbsp; With twitter, yo can eavesdrop on conversations, get tips in real time, and find out where people are hanging out at.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.youtube.com/watch?v=QDgFHZR7dAc&amp;amp;feature=related" target=_blank&gt;Here&lt;/A&gt; is a YouTube video that is pretty good about Twitter.&amp;nbsp; There are other ones out there as well.&lt;/P&gt;
&lt;P&gt;I recommend that you use a twitter client like TweetDeck on your PC and also on your smart phone.&amp;nbsp; The interface makes it easier to use than the plain vanilla web page.&amp;nbsp; In your twitter client, you can create custom "feeds" to target a particular word or idea.&amp;nbsp; You can have multiple comuns on your client with many of these "feeds" that you follow.&amp;nbsp; These words that you target are often referred to as hashtags.&amp;nbsp; Here are some common hashtags to follow:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;#sqlpass&lt;/LI&gt;
&lt;LI&gt;#sqlhelp&lt;/LI&gt;
&lt;LI&gt;#firsttimers&lt;/LI&gt;
&lt;LI&gt;#sqlkilt&lt;/LI&gt;
&lt;LI&gt;#sqlkaraoke&lt;/LI&gt;
&lt;LI&gt;#24hop&lt;/LI&gt;
&lt;LI&gt;#sqlrally&lt;/LI&gt;
&lt;LI&gt;#sqlsat&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Once you start using twitter, you''ll understand more of what all this means.&amp;nbsp; Brent Ozar has put together a great primer on twitter that may be found &lt;A href="http://www.brentozar.com/archive/2008/08/twitter-101/" target=_blank&gt;here&lt;/A&gt;.&amp;nbsp; Closing thoughts on Twitter:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You don't have to create an account to get value out of it.&amp;nbsp; You can just follow some of the hashtags listed above - especially #sqlpass&lt;/LI&gt;
&lt;LI&gt;Get on twitter and try it out before you get to the summit&lt;/LI&gt;
&lt;LI&gt;Follow #sqlhelp and learn a LOT about where you can find great resources for SQL Server.&lt;/LI&gt;
&lt;LI&gt;make up your own hashtags like #JustSayin or #KilroyWashere or #whatever&lt;/LI&gt;
&lt;LI&gt;When choosing a "handle", consider incorporating "SQL" as part of it.&amp;nbsp; Members of the SQL Community are more apt to follow you and value your tweets if they know you are a SQL Server Professional.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Another application (targeted for mobile devices) that can be valuable is FourSquare.&amp;nbsp; FourSquare also integrates with Twiiter and FaceBook.&amp;nbsp; It works best with smart phones with GPS enabled.&amp;nbsp; Essentially, you check-in to a location such as a restaurant , bar, Conference Center, etc.&amp;nbsp; As part of the "check-in" process, you can add a "shout" to your check-in that may be tweeted and/or set to update your FaceBook status.&amp;nbsp; This is optional, but it can help with networking.&amp;nbsp; Check out FourSquare.&lt;/P&gt;
&lt;P&gt;The third social networking property that holds value is LinkedIn.&amp;nbsp; There are many discussion groups affiliated with PASS.&amp;nbsp; It is a great way to connect with other SQL Professionals once you get home.&lt;/P&gt;
&lt;P&gt;There is also a &lt;A href="http://www.youtube.com/user/SQLPASSTV" target=_blank&gt;PASS YouTube&lt;/A&gt; Channel.&amp;nbsp; Check out the videos from last year's summit!&amp;nbsp; Of course, PASS is also on FaceBook.&amp;nbsp; Another social networking tool that you may find useful is YELP.&amp;nbsp; It is particualrly good at helping to find a good restaurant near by.&lt;/P&gt;
&lt;P&gt;Flickr - Many folks who are shutterbugs or just want to share photos do so via Flickr and the&amp;nbsp;#sqlpass or #PASS2011 hashtags.&amp;nbsp; I look forward to seeing a lot of pictures from everyone this year.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bottom Line:&amp;nbsp; Get on Twitter Now!&amp;nbsp; Follow #sqlpass and #sqlhelp.&amp;nbsp; Get involved in the conversation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item><item><title>PASS Summit 2011 - What you can expect at the Conference - Part 2</title><link>http://sqlblog.com/blogs/rick_heiges/archive/2011/09/12/what.aspx</link><pubDate>Mon, 12 Sep 2011 13:11:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:38404</guid><dc:creator>RickHeiges</dc:creator><description>&lt;P&gt;One of the things that you should do is map out your "Learning Itinerary" before coming to the summit. It probably will change once you get onsite, but having a plan will save you time and perhaps some frustration.&amp;nbsp; You can go to the Summit website and start to &lt;A href="http://www.sqlpass.org/summit/2011/SummitContent/BuildSchedule.aspx" target=_blank&gt;build&lt;/A&gt; your schedule.&lt;/P&gt;
&lt;P&gt;When you arrive at registration to get your badge and other materials, you will also receive a Program Guide.&amp;nbsp; This booklet has a lot of information about the entire event.&amp;nbsp; It has a detailed description of each session, maps of the venue, and much more.&amp;nbsp; We also have a Smart Phone App (iPhone, Android, Windows 7 Phone) that has much of this same information.&amp;nbsp; The advantage of relying on the Smat Phone App is that it will have the latest information available (Session Changes, etc).&amp;nbsp; More information on this later.&lt;/P&gt;
&lt;P&gt;Each Session has a Session Code associated with it. Example: AD-104, BIA-303-S, DBA-302-M.&amp;nbsp; There are 4 timeslots/day with up to 15 different choices; that's over 170 choices!&lt;/P&gt;
&lt;P&gt;The first part identifies the &lt;A href="http://www.sqlpass.org/summit/2011/SummitContent.aspx" target=_blank&gt;Track&lt;/A&gt; the session is part of:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;AD = Application Development&lt;/LI&gt;
&lt;LI&gt;AZ = SQL Azure&lt;/LI&gt;
&lt;LI&gt;BIA = BI Architecture / Development&lt;/LI&gt;
&lt;LI&gt;BID = BI Deployment / Delivery&lt;/LI&gt;
&lt;LI&gt;DBA = Enterprise Database Administration and Deployment&lt;/LI&gt;
&lt;LI&gt;PD = Professional Development&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The Second part indicates the Level that the session is aimed at.&amp;nbsp; 100, 200, and so on.&lt;/P&gt;
&lt;P&gt;The suffix is optional and may indicate several things to be aware of:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;S = &lt;A href="http://www.sqlpass.org/summit/2011/SummitContent/SpotlightSessions.aspx" target=_blank&gt;Spotlight&lt;/A&gt; Session (These sessions are 90 minutes instead of 75)&lt;/LI&gt;
&lt;LI&gt;M= Microsoft (Presented by Microsoft)&lt;/LI&gt;
&lt;LI&gt;C = CSS (Presented by a Member of the CSS team from Microsoft)&lt;/LI&gt;
&lt;LI&gt;HD = &lt;A href="http://www.sqlpass.org/summit/2011/SummitContent/ProgramSessions.aspx?cid=47&amp;amp;p=1&amp;amp;preferred=false" target=_blank&gt;Half-Day&lt;/A&gt; (3 hour session - NEW This Year!)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;There are also a few sessions identified as "&lt;A href="http://www.sqlpass.org/summit/2011/SummitContent/LightningTalks2011.aspx" target=_blank&gt;Lightning Talks&lt;/A&gt;"&amp;nbsp; These sessions have a variety of speakers who give a brief 5 minute presentation.&amp;nbsp; Lots of Fun and covers a multitude of subjects quickly.&amp;nbsp; You won't be bored in here!&amp;nbsp; I encourage you to attend at least one Lightning Talk during the summit.&amp;nbsp; You never know what gems you can find in a 5 minute talk.&lt;/P&gt;
&lt;P&gt;What happens if there is more than one session that you wish to attend?&amp;nbsp; Can you be two places at once?&amp;nbsp; No, but all sessions are being recorded and will be available usually a few weeks later via streaming.&amp;nbsp; A DVD set may also be purchased by modifying your registration information or by ordering onsite.&lt;/P&gt;
&lt;P&gt;Outside the session rooms, a sign will indicate what sessions are scheduled to be there and who is presenting.&amp;nbsp; Some sessinos / speakers will have a special designation.&amp;nbsp;Here are some terms that you may see and what it means:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;MVP - Microsoft Most Valuable Professional Awardee (This is a non-Microsoft person who eagerly shares knowledge about SQL Server with the community)&lt;/LI&gt;
&lt;LI&gt;MCM - Microsoft Certified Professional (Passed both a written and Lab Exam)&lt;/LI&gt;
&lt;LI&gt;SQLCAT - SQL Server Customer Advisory Team member (often weearing a SQLCAT shirt typically of a hideous color)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;In the hallways and lunch rooms, you will also see people with a Red Vest on.&amp;nbsp; These people are playing the role of a PASS Ambassador.&amp;nbsp; These people are Alumni Attendees who simply want to help you have a better experience at the summit.&amp;nbsp; They can help you with Finding a Session room, Reading the Program Guide, Getting you in touch with the right resource, or just about anything else.&lt;/P&gt;
&lt;P&gt;The Session Rooms are setup "Theater Style" - Rows of chairs beside one another (no tables).&amp;nbsp; You will notice that each session room will have several reserved seats for Amabassadors.&amp;nbsp; Ambassadors often come in late or leave a little early and need easy access to alleviate any&amp;nbsp;disruption to the speaker.&lt;/P&gt;
&lt;P&gt;Each Room also has a Room Monitor.&amp;nbsp; These people work for the convention center.&amp;nbsp; They are charged with:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Enforcing Capacity Restrictions / Fire Code&lt;/LI&gt;
&lt;LI&gt;Enforcing Security for Entry to the Session Rooms by checking badges&lt;/LI&gt;
&lt;LI&gt;Counting Attendees / Providing a 5 minute warning to the speaker&lt;/LI&gt;
&lt;LI&gt;Collection Paper Evaluations&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Room Monitors are helpful too - especially with questions about the facility.&amp;nbsp; Amabassadors are a better resource for questions relating to PASS or the summit in general.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item><item><title>Setting up for PASS Summit Orientation</title><link>http://sqlblog.com/blogs/rick_heiges/archive/2011/08/31/setting-up-for-pass-summit-orientation.aspx</link><pubDate>Wed, 31 Aug 2011 13:41:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:38181</guid><dc:creator>RickHeiges</dc:creator><description>&lt;P&gt;I am in the process of finalizing a date (week of Sep 19) for a LiveMeeting event that will focus on the upcoming PASS Community Summit.&amp;nbsp;It will be primarily targeted at the first-timers to the Summit, but everyone will be welcome to join.&amp;nbsp; There will be event information forthcoming on the PASS website - &lt;A href="http://www.sqlpass.org/"&gt;www.sqlpass.org&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;Here is a rundown on the topics that I plan to cover:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;What is PASS?&amp;nbsp; PASS Events (Summit, Rally, 24hop, SQLSat, Chapters, Virtual Chapters)&lt;/LI&gt;
&lt;LI&gt;What to Expect / How to read the Program Guide and Smart Phone App to Plan your Itinerary&lt;/LI&gt;
&lt;LI&gt;Pre-cons, half-day sessions, spotlight sessions, Speaker annotations (MVP, MCM, SQLCAT)&lt;/LI&gt;
&lt;LI&gt;Stuff besides Sessions (Hands-on labs, SQL Clinic, Expert Pods, Birds of Feather Lunch, etc.)&lt;/LI&gt;
&lt;LI&gt;Social Media (Twitter, Foursquare, Flickr, etc)&lt;/LI&gt;
&lt;LI&gt;Activities "outside of the Summit" (SQL Karaoke, Photo walk, Kilt Day, etc)&lt;/LI&gt;
&lt;LI&gt;Logistics - Getting to the Summit from SEA-TAC (light rail, shuttle, cab, etc.)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I have gone through the Flickr photos.&amp;nbsp; Always looking for more "gems".&amp;nbsp; Looking for photos that may help with logistics or showcasing interaction at the summit.&amp;nbsp;Leave a comment for a link to other sites like Flickr with pictures from past summits.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description></item><item><title>Chasing the ISV, or, “That code makes my teeth hurt.” T-SQL Tuesday (ish) #21</title><link>http://sqlblog.com/blogs/merrill_aldrich/archive/2011/08/10/ouch-chasing-the-isv-or-that-code-makes-my-teeth-hurt-t-sql-tuesday-ish-21.aspx</link><pubDate>Wed, 10 Aug 2011 14:00:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:37656</guid><dc:creator>merrillaldrich</dc:creator><description>&lt;p&gt;&lt;img style="display:inline;float:left;" title="" alt="Jenga Effect" align="left" src="http://upload.wikimedia.org/wikipedia/en/a/a9/Jenga.JPG" width="240" height="224" /&gt;This month’s &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2011/08/03/t-sql-tuesday-21-a-day-late-and-totally-full-of-it.aspx"&gt;T-SQL Tuesday&lt;/a&gt; – a blog party dreamed up by sqlblog.com’s Adam Machanic ( &lt;a href="http://sqlblog.com/blogs/adam_machanic/default.aspx"&gt;blog&lt;/a&gt; | @&lt;a href="http://twitter.com/#!/AdamMachanic"&gt;AdamMachanic&lt;/a&gt; ) – is about that code we’ve all written that we don’t really like to think about too often. You know the stuff. I can’t help but imagine the next poor guy who comes across some of mine and thinks, “What the … How in … Seriously?”&lt;/p&gt;  &lt;p&gt;I have two gems to share today. They share the theme, “Chasing the ISV,” because they are both SQL Agent jobs that essentially follow ISV code and constantly, 24 x 7, mop up problems with applications.&lt;/p&gt;  &lt;p&gt;The first example I’ll put up here is one I blogged about briefly &lt;a href="http://sqlblog.com/blogs/merrill_aldrich/archive/2010/05/20/worst-code-i-ve-written-in-a-while.aspx"&gt;last year&lt;/a&gt;. We had an ISV system that was riddled with pretty horrible triggers, which I was hesitant to touch for fear of the “Jenga effect.” Pull just one line of trigger code out and – who knows?&lt;/p&gt;  &lt;p&gt;Said system had a production issue because a value stored in two places, that was supposed to match (winning system here) no longer did. I discovered that the underlying reason was that the tables in play had triggers that could not handle a set of rows, but just one row at a time – a too-common anti pattern. &lt;/p&gt;  &lt;h2&gt;IF Problem GOTO Workaround&lt;/h2&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2011/08/03/t-sql-tuesday-21-a-day-late-and-totally-full-of-it.aspx"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;float:right;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="TSQLWednesday_2C948C01" border="0" alt="TSQLWednesday_2C948C01" align="right" src="http://sqlblog.com/blogs/merrill_aldrich/TSQLWednesday_2C948C01_5A7741FD.jpg" width="244" height="244" /&gt;&lt;/a&gt;So, what to do. Production issue. Deep breath. Here goes:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Again:

UPDATE TOP &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;1&lt;span style="color:gray;"&gt;) &lt;/span&gt;dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;someTable &lt;span style="color:blue;"&gt;SET &lt;/span&gt;field3 &lt;span style="color:gray;"&gt;= &lt;/span&gt;&lt;span style="color:red;"&gt;'NEW'
&lt;/span&gt;&lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;field2 &lt;span style="color:gray;"&gt;= &lt;/span&gt;&lt;span style="color:red;"&gt;'NEW' &lt;/span&gt;&lt;span style="color:gray;"&gt;AND &lt;/span&gt;field3 &lt;span style="color:gray;"&gt;= &lt;/span&gt;&lt;span style="color:red;"&gt;''

&lt;/span&gt;&lt;span style="color:blue;"&gt;IF &lt;/span&gt;&lt;span style="color:magenta;"&gt;@@ROWCOUNT &lt;/span&gt;&lt;span style="color:gray;"&gt;&amp;gt; &lt;/span&gt;0 &lt;span style="color:blue;"&gt;GOTO &lt;/span&gt;Again&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Into a SQL Agent job with that, executed once every few minutes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So. Wrong. &lt;/p&gt;

&lt;p&gt;Also fixed the issue. And it’s still in use, I believe. I try not to think about it.&lt;/p&gt;

&lt;h2&gt;IN ( Pain, Suffering )&lt;/h2&gt;

&lt;p&gt;The second bit of pure awesome I have to offer today takes a bit more explanation. I think I should anonymize this one to protect the guilty. (That first example has, believe it or not, the real column names.)&lt;/p&gt;

&lt;p&gt;Another ISV provided a system to one of my past employers that, let’s say for argument, provided a web application to manage distributed offices in locations all over the country. The structure of the system required that people at the company be able to view one or a few of these locations’ data, but not the data from other locations. So the system had a row-level security mechanism to filter the data by groups - a fairly standard idea, but theirs was not a typical implementation at all. The way the initial version of this software operated was, roughly:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An administrator for the application would change some aspect of the security groups using the web application. &lt;/li&gt;

  &lt;li&gt;The system would, upon clicking Submit, create (or recreate) one or several view definitions, inside of which there was a SELECT with a static IN (,,) list of any and all the locations that a given group or individual could see on the system. The view would provide the row-level security. The IN(,,) list could have a few or hundreds of items. A classic data-as-code anti-pattern. &lt;/li&gt;

  &lt;li&gt;The system was very busy, so altering the view would often fail or cause … “side effects.” &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The company actually instituted a rule that security changes like this could only be made after hours, and made staff stay late to do that work. If this happened during the day, the system would generally grind to a halt, and none of the 1,000 or so concurrent users could do anything. Why? The IN() lists in these views could have &lt;em&gt;hundreds&lt;/em&gt; of values, and they worked against millions of rows of data. That meant that with no meaningful stats to work with about these embedded lists, the query optimizer would &lt;em&gt;always&lt;/em&gt; pick a terrible query plan for vital processes on the system.&lt;/p&gt;

&lt;p&gt;The ISV almost fixed this problem. Nearly. &lt;/p&gt;

&lt;p&gt;When complaints surfaced, they went in and added a more scalable feature that would use a many-to-many table to store the security relationships. The table could be indexed, and would have stats, which was wonderful, and it solved the performance issue. &lt;/p&gt;

&lt;p&gt;When they used it.&lt;/p&gt;

&lt;p&gt;Here’s the rub: the were so fearful about disrupting their customers’ existing deployments – a noble but ultimately doomed posture – that they kept the old view mechanism too. Any old features of the product used the old system, while new or rewritten features used the new system. Now their code had to maintain both systems, and attempt to be sure they matched. Which they did. Mostly. Except that time we had to have one of their developers spend a week untangling the thing. But I digress.&lt;/p&gt;

&lt;p&gt;If I recall, and this was a few years ago, so I may have this wrong, it seems like their first table-based implementation didn’t exactly work, and they had to create a &lt;em&gt;second&lt;/em&gt; one, and then they kept all three systems, with some massive stored procs to keep them all in sync. And the proc worked, except when there was a logic problem like duplicate rows in the tables, and then it didn’t anymore.&lt;/p&gt;

&lt;h2&gt;ALTER VIEW dbo.FixMe&lt;/h2&gt;

&lt;p&gt;This is where our little DBA group came flying in with our superhero capes and a brilliant, yet horrible, workaround. I can’t quite remember who created this, so my apologies if I cannot credit the specific individual, but I know I, erm, “tuned” it over the years. I also vividly remember the phone calls and the shouting when this workaround was not operating as designed.&lt;/p&gt;

&lt;p&gt;We made a SQL Agent job to run every few minutes. The code for the job was, essentially:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Examine the text for all the views in the database, via syscomments, where the view name matched some text pattern that makes it look like one of these security views. &lt;/li&gt;

  &lt;li&gt;If the view definition contains one of these IN(,,) clauses then rewrite the view, via dynamic SQL, as a join between the security tables. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Like the example above, this job would basically sit there and chase the underlying system, and if a security change was made, it would locate the offending view definition and rewire it on the fly to use the more performant and optimizer-friendly tables.&lt;/p&gt;

&lt;p&gt;I think the vendor has since fixed the view definition issue. Happily, I don’t have to work on this system any longer, so I can’t say whether the views themselves persist to this day.&lt;/p&gt;

&lt;p&gt;Here’s the code, unvarnished but anonymized. I found it on an old flash drive:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;IF &lt;/span&gt;&lt;span style="color:gray;"&gt;EXISTS ( &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:gray;"&gt;* &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:green;"&gt;syscomments &lt;/span&gt;sc1    
   &lt;span style="color:gray;"&gt;INNER JOIN &lt;/span&gt;&lt;span style="color:green;"&gt;sysobjects &lt;/span&gt;so &lt;span style="color:blue;"&gt;ON &lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;id &lt;span style="color:gray;"&gt;= &lt;/span&gt;sc1&lt;span style="color:gray;"&gt;.&lt;/span&gt;id   
   &lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;type &lt;/span&gt;&lt;span style="color:gray;"&gt;=&lt;/span&gt;&lt;span style="color:red;"&gt;'V'    
      &lt;/span&gt;&lt;span style="color:gray;"&gt;AND &lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;name &lt;span style="color:gray;"&gt;LIKE &lt;/span&gt;&lt;span style="color:red;"&gt;'FOO\_%' &lt;/span&gt;&lt;span style="color:blue;"&gt;ESCAPE &lt;/span&gt;&lt;span style="color:red;"&gt;'\'    
      &lt;/span&gt;&lt;span style="color:gray;"&gt;AND &lt;/span&gt;sc1&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;text &lt;/span&gt;&lt;span style="color:gray;"&gt;LIKE &lt;/span&gt;&lt;span style="color:red;"&gt;'% IN (%)'   &lt;/span&gt;&lt;span style="color:gray;"&gt;) 
&lt;/span&gt;&lt;span style="color:blue;"&gt;BEGIN     
   SELECT     &lt;/span&gt;&lt;span style="color:red;"&gt;'ALTER VIEW dbo.' &lt;/span&gt;&lt;span style="color:gray;"&gt;+ &lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;name &lt;span style="color:gray;"&gt;+ 
      &lt;/span&gt;&lt;span style="color:red;"&gt;' AS SELECT f.* FROM dbo.foo f, bar l ' &lt;/span&gt;&lt;span style="color:gray;"&gt;+
      &lt;/span&gt;&lt;span style="color:red;"&gt;' WHERE f.id = l.id ' &lt;/span&gt;&lt;span style="color:gray;"&gt;+
      &lt;/span&gt;&lt;span style="color:red;"&gt;' AND l.id = '     &lt;/span&gt;&lt;span style="color:gray;"&gt;+  
       RIGHT(&lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;name&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:magenta;"&gt;len&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;name&lt;span style="color:gray;"&gt;) - &lt;/span&gt;&lt;span style="color:magenta;"&gt;charindex&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:red;"&gt;'_'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;name&lt;span style="color:gray;"&gt;))    
            &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;alterViewCommand     
   &lt;span style="color:blue;"&gt;INTO &lt;/span&gt;#viewCorrections     
   &lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:green;"&gt;syscomments &lt;/span&gt;sc1     
   &lt;span style="color:gray;"&gt;INNER JOIN &lt;/span&gt;&lt;span style="color:green;"&gt;sysobjects &lt;/span&gt;so &lt;span style="color:blue;"&gt;ON &lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;id &lt;span style="color:gray;"&gt;= &lt;/span&gt;sc1&lt;span style="color:gray;"&gt;.&lt;/span&gt;id   
      &lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;type &lt;/span&gt;&lt;span style="color:gray;"&gt;=&lt;/span&gt;&lt;span style="color:red;"&gt;'V'    
      &lt;/span&gt;&lt;span style="color:gray;"&gt;AND &lt;/span&gt;so&lt;span style="color:gray;"&gt;.&lt;/span&gt;name &lt;span style="color:gray;"&gt;LIKE &lt;/span&gt;&lt;span style="color:red;"&gt;'FOO\_%' &lt;/span&gt;&lt;span style="color:blue;"&gt;ESCAPE &lt;/span&gt;&lt;span style="color:red;"&gt;'\'    
      &lt;/span&gt;&lt;span style="color:gray;"&gt;AND &lt;/span&gt;sc1&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;text &lt;/span&gt;&lt;span style="color:gray;"&gt;like &lt;/span&gt;&lt;span style="color:red;"&gt;'% IN (%)'     

   &lt;/span&gt;&lt;span style="color:blue;"&gt;DECLARE &lt;/span&gt;@sql &lt;span style="color:blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;4000&lt;span style="color:gray;"&gt;)     

   &lt;/span&gt;&lt;span style="color:blue;"&gt;WHILE &lt;/span&gt;&lt;span style="color:gray;"&gt;EXISTS( &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;1 &lt;span style="color:blue;"&gt;FROM &lt;/span&gt;#viewCorrections &lt;span style="color:gray;"&gt;)    
   &lt;/span&gt;&lt;span style="color:blue;"&gt;BEGIN
         SELECT TOP &lt;/span&gt;1 @sql &lt;span style="color:gray;"&gt;= &lt;/span&gt;alterViewCommand &lt;span style="color:blue;"&gt;FROM &lt;/span&gt;#viewCorrections

         &lt;span style="color:blue;"&gt;PRINT &lt;/span&gt;@sql

         &lt;span style="color:blue;"&gt;EXEC &lt;/span&gt;&lt;span style="color:gray;"&gt;( &lt;/span&gt;@sql &lt;span style="color:gray;"&gt;)

         &lt;/span&gt;&lt;span style="color:blue;"&gt;DELETE FROM &lt;/span&gt;#viewCorrections &lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;alterViewCommand &lt;span style="color:gray;"&gt;= &lt;/span&gt;@sql

       &lt;span style="color:blue;"&gt;WAITFOR DELAY &lt;/span&gt;&lt;span style="color:red;"&gt;'000:00:05'
    &lt;/span&gt;&lt;span style="color:blue;"&gt;END

    DROP TABLE &lt;/span&gt;#viewCorrections
&lt;span style="color:blue;"&gt;END&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;</description></item><item><title>OT: March Mdness 2011</title><link>http://sqlblog.com/blogs/rick_heiges/archive/2011/03/14/ot-march-mdness-2011.aspx</link><pubDate>Mon, 14 Mar 2011 11:11:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34110</guid><dc:creator>RickHeiges</dc:creator><description>&lt;P&gt;This past fall, I decided to take a break from Fantasy Football.&amp;nbsp; Did I miss it? Yes to some extent.&amp;nbsp; Fantasy Football can really eat up a lot of time.&lt;/P&gt;
&lt;P&gt;But - I still love March Madness (NCAA Men's Basketball Tourney).&amp;nbsp; It doesn't take much time to pick out teams.&amp;nbsp; Since you can't make any changes after the deadline and the computer keeps track of scoring/scenarios/etc, it is a fun thing that really takes a little time and can help you enjoy the games a bit more.&amp;nbsp; Let's see how good you are at picking winners with others within the SQL Community.&amp;nbsp; The number of slots is unlimited.&amp;nbsp; Here is the link...&lt;/P&gt;
&lt;P&gt;&lt;A href="http://tournament.fantasysports.yahoo.com/t1/register/joinprivategroup_assign_team?GID=62763&amp;amp;P=sqlblog"&gt;http://tournament.fantasysports.yahoo.com/t1/register/joinprivategroup_assign_team?GID=62763&amp;amp;P=sqlblog&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;The password is "sqlblog".&amp;nbsp; Fill out a bracket and compete for bragging rights!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item><item><title>Sudoku Solver</title><link>http://sqlblog.com/blogs/merrill_aldrich/archive/2010/04/17/sudoku-solver.aspx</link><pubDate>Sun, 18 Apr 2010 00:16:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:24377</guid><dc:creator>merrillaldrich</dc:creator><description>&lt;P&gt;Today I am putting up something silly, just for fun. I set myself the task a while back to write a Sudoku solver in T-SQL, but with two dumb constraints that I would never follow given a real problem: I didn’t look at any documented techniques for solving Sudoku, and I specifically avoided T-SQL solutions, even though this has been done already many times. (The first thing I do with a real problem is to see who solved it already, and how, since most things have been done already. Not checking is a sure way to get inducted into The International Society of Wheel Re-inventors®.) So this solution is quite naive and probably has some gaps in it. I imagine it does the same thing as some other solvers, but, you’ll have to trust me, that’s a coincidence. Still, it gave me something to do evenings while my wife was doing … erm … Sudoku.&lt;/P&gt;
&lt;P&gt;Anyway, the challenge I set for myself was to try to make a solver that:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Is completely set-based, in the spirit of both SQL and Sudoku. &lt;/LI&gt;
&lt;LI&gt;Has as few statements as possible. I actually tried to write a single recursive CTE to solve a whole puzzle, but I didn’t quite get there, because I ran up against a limitation where you cannot reference a recursive member more than once in a CTE, and I’m not quite smart enough to solve that. I still have a hunch that it’s possible. The solution I do have consists of two statements with some flow control around them. &lt;/LI&gt;
&lt;LI&gt;Should be reasonable to compute – that is, it’s not a brute-force method. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;At a high level, the algorithm I came up with follows this logic: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The set of digits 1-9&amp;nbsp;is loaded into a numbers table “allValues.” &lt;/LI&gt;
&lt;LI&gt;The set of all possible “cells” – all the boxes in the puzzle –&amp;nbsp;is loaded into a second numbers-like table, “allCells.” This table just provides the address of each container / cell in the puzzle, to facilitate creating a left-join that will produce all the cells in a whole solution. &lt;/LI&gt;
&lt;LI&gt;A puzzle is loaded into a third table, “puzzle,” which will start with the given values in the puzzle and ultimately hold the solution. This table is constrained with the same rules as the paper puzzle, so constraints enforce that it’s not possible to insert values into cells that break the rules of Sudoku. &lt;/LI&gt;
&lt;LI&gt;A solver script is run against the tables: &lt;/LI&gt;&lt;/OL&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;While the puzzle is unsolved, repeat:&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;OL&gt;
&lt;OL&gt;
&lt;LI&gt;Compute the set of &lt;EM&gt;all&lt;/EM&gt; possible values that can be inserted into &lt;EM&gt;all&lt;/EM&gt; empty cells in the puzzle without breaking any rule&lt;/LI&gt;
&lt;LI&gt;From that set, select any cells/values that are solvable because either there is only one possible value for the cell OR there is only one possible location for a given digit 0-9 in a row, column, or quadrant. That is, fill in cells that have deterministic solutions based on the existing values in the puzzle. (This usually is all that is required to solve an “easy” level puzzle, but won’t complete for a hard one.) Insert these deterministic values into the puzzle table. &lt;/LI&gt;
&lt;LI&gt;IF there are no results from the prior step, that is, there are no solve-able cells, then: 
&lt;OL&gt;
&lt;LI&gt;Branch the solution by making a copy of it, so that there are two versions in the puzzle table. One ultimately will be correct, and one will be unsolvable. &lt;/LI&gt;
&lt;LI&gt;Find one cell within the whole set of possible values that has two possible answers, and put one possibility into the first version, and the second possibility into the second version. &lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;
&lt;LI&gt;IF the insert of solved cells from step 2 &lt;EM&gt;failed&lt;/EM&gt;, due to a constraint violation, then that implies that one of the branched versions of the puzzle in the table has reached an impasse: that is, it’s incorrect and unsolvable. Delete this version, leaving the correct one, and continue. &lt;/LI&gt;&lt;/OL&gt;&lt;/OL&gt;
&lt;H2&gt;Setup&lt;/H2&gt;
&lt;P&gt;Designing the structure of the puzzle table was a little tricky. As we know, relational tables are not like actual grids because the order of both the rows and columns is undefined. All one can say about the values in a relation is that they are in the same row or in the same column, but there is no relative position as there is in a grid. So as tempting as it seems, we can’t represent the puzzle grid literally, shoving it into a table, because the positions of the cells in the paper grid carry meaning, while the positions of values in a relational table can’t. Basically, in SQL Server there is no “first” row or “first” column, and value cannot be said to be “next to” another value.&lt;/P&gt;
&lt;P&gt;So, I arrived at a design that normalizes the data from the puzzle by creating a separate row for each cell in the whole puzzle, basically un-pivoting the grid of cells from the paper grid into a list. Each row in the resulting table stores an address for the position in the paper puzzle as Row, Column and Quadrant (the nine-square regions within the puzzle), with the digit in that cell as “Value.” The quadrant is obviously determined by the (row, column) pair and is deterministic, so that can be calculated using a computed column. The table definition looks like this:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;CREATE TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;(
    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:blue;"&gt;tinyint &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT NULL,
    &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:blue;"&gt;tinyint &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT NULL,
    &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:blue;"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;1&lt;SPAN style="COLOR:gray;"&gt;) NOT NULL,
    &lt;/SPAN&gt;Quadrant  &lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
        (&lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'Q-' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;+ 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;case when &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;3 &lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'A' 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;when &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;4 &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;6 &lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'B' 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;when &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;7 &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;9 &lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'C'  &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;end &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
        + &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;case when &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'A' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'C' &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'A' 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;when &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'D' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'F' &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'B' 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;when &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'G' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'I' &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'C'  &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;end &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;),
    &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:blue;"&gt;tinyint &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT NULL
)&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Next, I added some rules to that table that make it mimic the “rules” implied by position in the paper version of the puzzle: no two cells in the same row, column, or quadrant can contain the same digit:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;CREATE UNIQUE NONCLUSTERED INDEX &lt;/SPAN&gt;ValsInaColumnAreUnique &lt;SPAN style="COLOR:blue;"&gt;ON &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle 
&lt;SPAN style="COLOR:gray;"&gt;(
    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:blue;"&gt;ASC&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,
    &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:blue;"&gt;ASC&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,
    &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:blue;"&gt;ASC
&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;CREATE UNIQUE NONCLUSTERED INDEX &lt;/SPAN&gt;ValsInaRowAreUnique &lt;SPAN style="COLOR:blue;"&gt;ON &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle 
&lt;SPAN style="COLOR:gray;"&gt;(
    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:blue;"&gt;ASC&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,
    &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:blue;"&gt;ASC&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,
    &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:blue;"&gt;ASC
&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;CREATE UNIQUE NONCLUSTERED INDEX &lt;/SPAN&gt;ValsInaQuadAreUnique &lt;SPAN style="COLOR:blue;"&gt;ON &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle 
&lt;SPAN style="COLOR:gray;"&gt;(
    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:blue;"&gt;ASC&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,
    &lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:blue;"&gt;ASC&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,
    &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:blue;"&gt;ASC
&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;ALTER TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle  &lt;SPAN style="COLOR:blue;"&gt;WITH CHECK ADD  CONSTRAINT &lt;/SPAN&gt;[ColIsA-I] &lt;SPAN style="COLOR:blue;"&gt;CHECK  &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;like &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'[A-I]' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;
ALTER TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:blue;"&gt;CHECK CONSTRAINT &lt;/SPAN&gt;[ColIsA-I]
&lt;SPAN style="COLOR:blue;"&gt;
ALTER TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle  &lt;SPAN style="COLOR:blue;"&gt;WITH CHECK ADD  CONSTRAINT &lt;/SPAN&gt;[RowIs1-9] &lt;SPAN style="COLOR:blue;"&gt;CHECK  &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;9 &lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;ALTER TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:blue;"&gt;CHECK CONSTRAINT &lt;/SPAN&gt;[RowIs1-9]
&lt;SPAN style="COLOR:blue;"&gt;
ALTER TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle  &lt;SPAN style="COLOR:blue;"&gt;WITH CHECK ADD  CONSTRAINT &lt;/SPAN&gt;[ValueIs1-9] &lt;SPAN style="COLOR:blue;"&gt;CHECK  &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;9 &lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;ALTER TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:blue;"&gt;CHECK CONSTRAINT &lt;/SPAN&gt;[ValueIs1-9]
&lt;FONT color=#0000ff&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;The allCells table is similar, but without the need for a SolutionID or Values column – it’s just a list of all the cells in one puzzle grid. The computed column for Quadrant is the same:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;CREATE TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;(
    &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:blue;"&gt;tinyint &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT NULL,
    &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:blue;"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;1&lt;SPAN style="COLOR:gray;"&gt;) NOT NULL,
    &lt;/SPAN&gt;Quadrant  &lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
        (&lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'Q-' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;+ 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;case when &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;3 &lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'A' 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;when &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;4 &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;6 &lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'B' 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;when &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;7 &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;9 &lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'C'  &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;end &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
        + &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;case when &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'A' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'C' &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'A' 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;when &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'D' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'F' &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'B' 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;when &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;BETWEEN &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'G' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'I' &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;then &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'C'  &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;end &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;),
) &lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allCells &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;1&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;N'A'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allCells &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;2&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;N'A'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allCells &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;3&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;N'A'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allCells &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;4&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;N'A'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;…&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allCells &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;8&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;N'I'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allCells &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;9&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;N'I'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Lastly, the numbers table:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;CREATE TABLE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;(
    &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:blue;"&gt;int &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT NULL
)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allValues &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Value&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;1&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allValues &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Value&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;2&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;…&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allValues &lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Value&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;9&lt;SPAN style="COLOR:gray;"&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:gray;"&gt;
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Loading a puzzle is a matter of running inserts against the Puzzle table to put in the given values, with SolutionID 0:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT INTO &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;0&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;1&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'B'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;3 &lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT INTO &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;0&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;1&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'I'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;8 &lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT INTO &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;0&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;2&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'A'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;2 &lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT INTO &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;0&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;2&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'B'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;4 &lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;…&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT INTO &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;0&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;2&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'I'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;6 &lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the Puzzle table doesn’t contain “empty” cells, only solved cells, and it cannot contain invalid values. It might, however, have an unsolvable puzzle even though each value present doesn’t violate the rules – a fact that will become important later.&lt;/P&gt;
&lt;H2&gt;Viewing a Puzzle&lt;/H2&gt;
&lt;P&gt;Next, it’s important (and more fun!) to be able to view the puzzle in a grid, as it looks on paper. I struggled with this a bit until realizing that this is basically a pivot problem, and the pivot operator can be used to rotate the puzzle table back into the grid:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;[A]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[B]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[C]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[D]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[E]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[F]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[G]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[H]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[I]
&lt;SPAN style="COLOR:blue;"&gt;FROM
&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
    &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;allCells
    &lt;SPAN style="COLOR:gray;"&gt;LEFT JOIN &lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:blue;"&gt;on &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;row &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;and &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;col &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;col
    &lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;0
&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;sourceTable &lt;SPAN style="COLOR:gray;"&gt;PIVOT(
    &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;min&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;for &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;in ( &lt;/SPAN&gt;[A]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[B]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[C]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[D]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[E]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[F]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[G]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[H]&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;[I] &lt;SPAN style="COLOR:gray;"&gt;)
) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;PivotTable
&lt;SPAN style="COLOR:blue;"&gt;ORDER BY &lt;/SPAN&gt;Row&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Only the non-null cells in the puzzle are present in the Puzzle table, but that set gets expanded via left-join to the allCells table to make a derived table with all 81 cells. That derived table is then pivoted on the columns to create nine columns by nine rows, and the resulting grid looks like the paper puzzle:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/merrill_aldrich/SudokuGrid_62ABABFA.jpg"&gt;&lt;IMG style="BORDER-BOTTOM:0px;BORDER-LEFT:0px;DISPLAY:inline;BORDER-TOP:0px;BORDER-RIGHT:0px;" title=SudokuGrid border=0 alt=SudokuGrid src="http://sqlblog.com/blogs/merrill_aldrich/SudokuGrid_thumb_23740485.jpg" width=440 height=225&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;Sudoku Solver&lt;/H2&gt;
&lt;P&gt;At this point, we can check out the solver. The solver consists basically of two (evil, complicated) statements: one that can insert solved cells into the puzzle table, and a second that can detect and delete an unsolvable version of a puzzle.&amp;nbsp; Those two statements are connected by flow control: a While loop that keeps the script going until the puzzle is solved, and a Try/Catch that handles the scenario where a version of a puzzle becomes unsolvable. &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR:green;"&gt;-- Loop while there is no solution having all 81 cells solved:
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHILE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT EXISTS ( 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;SolutionID 
    &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;Puzzle 
    &lt;SPAN style="COLOR:blue;"&gt;GROUP BY &lt;/SPAN&gt;SolutionID 
    &lt;SPAN style="COLOR:blue;"&gt;HAVING &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) = &lt;/SPAN&gt;81 
&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN
    BEGIN TRY&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- Try to insert new solved cells into the puzzle table:
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WITH 

        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- Every possible new value, for every empty cell in every puzzle version, 
        -- that does not contradict the solved cells already present in the puzzle table:
        &lt;/SPAN&gt;allPossibleNewValues
          &lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT   &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value
               &lt;SPAN style="COLOR:blue;"&gt;FROM     &lt;/SPAN&gt;allValues
                        &lt;SPAN style="COLOR:gray;"&gt;CROSS JOIN &lt;/SPAN&gt;allCells
                        &lt;SPAN style="COLOR:gray;"&gt;CROSS JOIN ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT DISTINCT
                                            &lt;/SPAN&gt;SolutionID
                                     &lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle
                                   &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;allSolutions
                        &lt;SPAN style="COLOR:gray;"&gt;LEFT JOIN &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:blue;"&gt;ON &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                             &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                             &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;allcells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
               &lt;SPAN style="COLOR:blue;"&gt;WHERE    &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- The cell isn't already solved:
                        &lt;/SPAN&gt;puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;IS NULL
                        
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- And there's no cell in the same row having the given value
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                         &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;puzzle r
                                         &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;r&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;r&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allcells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;r&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
                                                
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- And there's no cell in the same column having the given value                    
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                         &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;puzzle c
                                         &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;c&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;c&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allcells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;c&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
                                                
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- And there's no cell in the same quadrant having the given value
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                         &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;puzzle q
                                         &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;q&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;q&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;quadrant
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;q&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
             ),

        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- There are two sets that give us new solved cells to insert into the puzzle: 
        
        -- SureThings are values for empty cells, where the existing values already solved
        -- in the puzzle imply than only one value can go in the cell (there's literally only  
        -- one possibility for the unsolved cell, so it has to be correct):

        &lt;/SPAN&gt;sureThings
          &lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT   &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;MIN&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;Value&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;Value
               &lt;SPAN style="COLOR:blue;"&gt;FROM     &lt;/SPAN&gt;allPossibleNewValues
               &lt;SPAN style="COLOR:blue;"&gt;GROUP BY &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Quadrant
               &lt;SPAN style="COLOR:blue;"&gt;HAVING   &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) = &lt;/SPAN&gt;1
             &lt;SPAN style="COLOR:gray;"&gt;),

        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- ForcedDigits are values determined by process of elimination: each digit 1-9 has
        -- to find a "home" in every row, every column and every quadrant. That means that
        -- if a value appears only one time in the set of all possible values for any row, 
        -- column or quadrant, then it has to be the solution for that cell:
     
        &lt;/SPAN&gt;forcedDigits
          &lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT   &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Value
               &lt;SPAN style="COLOR:blue;"&gt;FROM     &lt;/SPAN&gt;allPossibleNewValues n
               &lt;SPAN style="COLOR:blue;"&gt;WHERE    
            
            &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- There is no other location in the same row that can have the given value
            -- IOW, the given value must go in a cell, because it cannot go anyplace else
            
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                     &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;allPossibleNewValues rows1
                                              &lt;SPAN style="COLOR:blue;"&gt;UNION &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;ALL
                                              &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle rows2
                                            &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;[rows]
                                     &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;[rows]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[rows]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[rows]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[rows]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
                        OR

            &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- There is no other location in the same column where the given value can go
            
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                     &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;allPossibleNewValues cols1
                                              &lt;SPAN style="COLOR:blue;"&gt;UNION &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;ALL
                                              &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle cols2
                                            &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;[cols]
                                     &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;[cols]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[cols]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[cols]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[cols]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
                        OR
            
            &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- There is no other cell in the same quadrant where the given value can go
            
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                     &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;allPossibleNewValues quads1
                                              &lt;SPAN style="COLOR:blue;"&gt;UNION &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;ALL
                                              &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle quads2
                                            &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;[quads]
                                     &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;quadrant &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;quadrant
                                            &lt;SPAN style="COLOR:gray;"&gt;AND ( &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                                                  &lt;SPAN style="COLOR:gray;"&gt;OR &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                                &lt;SPAN style="COLOR:gray;"&gt;)
                                            AND &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
             )
                
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT  INTO &lt;/SPAN&gt;Puzzle
                &lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                  &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                  &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                  &lt;/SPAN&gt;Value 
                &lt;SPAN style="COLOR:gray;"&gt;)
                
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- New solved cells, if there are any
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;Value 
         &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
                &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT  &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Value
                &lt;SPAN style="COLOR:blue;"&gt;FROM    &lt;/SPAN&gt;sureThings
                &lt;SPAN style="COLOR:blue;"&gt;UNION
                SELECT  &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Value
                &lt;SPAN style="COLOR:blue;"&gt;FROM    &lt;/SPAN&gt;forcedDigits
        &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;as &lt;/SPAN&gt;solutions
        
        &lt;SPAN style="COLOR:blue;"&gt;UNION
        
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- Otherwise, branch by duplicating the existing solution as (solutionid + 1) plus
        -- insert one trial cell value into the existing solution, and another trial value 
        -- into the new solution:

        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
            &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
            &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
            &lt;/SPAN&gt;Value
        &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
            &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- A copy of the existing version of the puzzle:
            &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;MAX&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;from &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:gray;"&gt;) + &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:blue;"&gt;as &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;Value
            &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle
            
            &lt;SPAN style="COLOR:blue;"&gt;UNION
            
            &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- One each of two possible values for a cell - one directed to the existing
            -- solution and one to the copy, by Row_Number():
            &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;MIN&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:gray;"&gt;) 
                + ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;ROW_NUMBER&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;() &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;OVER &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ORDER BY &lt;/SPAN&gt;split&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;split&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;) - &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;as &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;split&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;split&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                &lt;/SPAN&gt;split&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value
            &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;allPossibleNewValues split
                &lt;SPAN style="COLOR:gray;"&gt;INNER JOIN ( 
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT TOP &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col 
                    &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;allPossibleNewValues 
                    &lt;SPAN style="COLOR:blue;"&gt;GROUP BY &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col
                    &lt;SPAN style="COLOR:blue;"&gt;HAVING &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) = &lt;/SPAN&gt;2
                    &lt;SPAN style="COLOR:blue;"&gt;ORDER BY &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col 
                &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;as &lt;/SPAN&gt;firstPair &lt;SPAN style="COLOR:blue;"&gt;on &lt;/SPAN&gt;split&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;firstPair&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row 
                    &lt;SPAN style="COLOR:gray;"&gt;and &lt;/SPAN&gt;split&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;firstPair&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                    
        &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;copyPuzzle
        
        &lt;SPAN style="COLOR:green;"&gt;-- Only in cases where there were no new solved cells 
        -- in sureThings or forcedDigits
        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;sureThings &lt;SPAN style="COLOR:gray;"&gt;) = &lt;/SPAN&gt;0 
            &lt;SPAN style="COLOR:gray;"&gt;and ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;forcedDigits &lt;SPAN style="COLOR:gray;"&gt;) = &lt;/SPAN&gt;0&lt;SPAN style="COLOR:gray;"&gt;;

    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END TRY

    BEGIN CATCH&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
    &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- If the insert above failed due to constraint violation, that means that
    -- one version of the puzzle is contradictory/unsolvable.
    -- Find and delete the solution at an impasse:
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WITH 

        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- Every possible new value, for every empty cell, that does not contradict the
        -- solved cells already present in the puzzle (same logic as above):

        &lt;/SPAN&gt;allPossibleNewValues
          &lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT   &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value
               &lt;SPAN style="COLOR:blue;"&gt;FROM     &lt;/SPAN&gt;allValues
                        &lt;SPAN style="COLOR:gray;"&gt;CROSS JOIN &lt;/SPAN&gt;allCells
                        &lt;SPAN style="COLOR:gray;"&gt;CROSS JOIN ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT DISTINCT
                                            &lt;/SPAN&gt;SolutionID
                                     &lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle
                                   &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;allSolutions
                        &lt;SPAN style="COLOR:gray;"&gt;LEFT JOIN &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle &lt;SPAN style="COLOR:blue;"&gt;ON &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                             &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                             &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;allcells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;Puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
               &lt;SPAN style="COLOR:blue;"&gt;WHERE    &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- The cell isn't already solved:
                        &lt;/SPAN&gt;puzzle&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;IS NULL
                        
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- And there's no cell in the same row having the given value
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                         &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;puzzle r
                                         &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;r&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;r&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allcells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;r&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
                                                
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- And there's no cell in the same column having the given value                    
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                         &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;puzzle c
                                         &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;c&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;c&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allcells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;c&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
                                                
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- And there's no cell in the same quadrant having the given value
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                         &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;puzzle q
                                         &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;q&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allSolutions&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;q&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allCells&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;quadrant
                                                &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;q&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;allValues&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
             ),


        &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- ForcedDigits are values determined by process of elimination: each digit 1-9 has
        -- to find a "home" in every row (same logic as above):
     
        &lt;/SPAN&gt;forcedDigits
          &lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT   &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;,
                        &lt;/SPAN&gt;Value
               &lt;SPAN style="COLOR:blue;"&gt;FROM     &lt;/SPAN&gt;allPossibleNewValues n
               &lt;SPAN style="COLOR:blue;"&gt;WHERE    
            
            &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- There is no other location in the same row that can have the given value
            -- IOW, the given value must go in a cell, because it cannot go anyplace else
            
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                     &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;allPossibleNewValues rows1
                                              &lt;SPAN style="COLOR:blue;"&gt;UNION &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;ALL
                                              &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle rows2
                                            &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;[rows]
                                     &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;[rows]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[rows]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[rows]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[rows]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
                        OR

            &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- There is no other location in the same column where the given value can go
            
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                     &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;allPossibleNewValues cols1
                                              &lt;SPAN style="COLOR:blue;"&gt;UNION &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;ALL
                                              &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle cols2
                                            &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;[cols]
                                     &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;[cols]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[cols]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[cols]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[cols]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
                        OR
            
            &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- There is no other cell in the same quadrant where the given value can go
            
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT EXISTS ( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;*
                                     &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM   &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;allPossibleNewValues quads1
                                              &lt;SPAN style="COLOR:blue;"&gt;UNION &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;ALL
                                              &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT    &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Quadrant &lt;SPAN style="COLOR:gray;"&gt;,
                                                        &lt;/SPAN&gt;Value
                                              &lt;SPAN style="COLOR:blue;"&gt;FROM      &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle quads2
                                            &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;[quads]
                                     &lt;SPAN style="COLOR:blue;"&gt;WHERE  &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;SolutionID
                                            &lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;quadrant &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;quadrant
                                            &lt;SPAN style="COLOR:gray;"&gt;AND ( &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col &lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Col
                                                  &lt;SPAN style="COLOR:gray;"&gt;OR &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row &lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Row
                                                &lt;SPAN style="COLOR:gray;"&gt;)
                                            AND &lt;/SPAN&gt;[quads]&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;= &lt;/SPAN&gt;n&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Value &lt;SPAN style="COLOR:gray;"&gt;)
             )
                &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;DELETE &lt;/SPAN&gt;dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;Puzzle 
                
                &lt;SPAN style="COLOR:green;"&gt;-- An unsolvable puzzle version at an impasse will have an impossible 
                -- result for forcedDigits. That is, two values forced into one cell,
                -- or same values forced into the same row, column, or quadrant. Find
                -- the unsolvable version of the puzzle to delete:
                &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= (
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT TOP &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col 
                        &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;forcedDigits
                        &lt;SPAN style="COLOR:blue;"&gt;GROUP BY &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col
                        &lt;SPAN style="COLOR:blue;"&gt;HAVING &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) &amp;gt; &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;as &lt;/SPAN&gt;impasse1
                    &lt;SPAN style="COLOR:gray;"&gt;)
                OR &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= (
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT TOP &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value 
                        &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;forcedDigits 
                        &lt;SPAN style="COLOR:blue;"&gt;GROUP BY &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Row&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value
                        &lt;SPAN style="COLOR:blue;"&gt;HAVING &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) &amp;gt; &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;impasse2
                    &lt;SPAN style="COLOR:gray;"&gt;)
                OR &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= (
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT TOP &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value 
                        &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;forcedDigits 
                        &lt;SPAN style="COLOR:blue;"&gt;GROUP BY &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Col&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value
                        &lt;SPAN style="COLOR:blue;"&gt;HAVING &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) &amp;gt; &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;as &lt;/SPAN&gt;impasse3
                    &lt;SPAN style="COLOR:gray;"&gt;)
                OR &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:gray;"&gt;= (
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT TOP &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;SolutionID &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
                        &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Quadrant&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value 
                        &lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;forcedDigits 
                        &lt;SPAN style="COLOR:blue;"&gt;GROUP BY &lt;/SPAN&gt;SolutionID&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Quadrant&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;Value
                        &lt;SPAN style="COLOR:blue;"&gt;HAVING &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;COUNT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(*) &amp;gt; &lt;/SPAN&gt;1 &lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;impasse4
                    &lt;SPAN style="COLOR:gray;"&gt;);
    &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END CATCH&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;

&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;So far this has solved each puzzle I’ve fed into it. If I were a mathematician, I suppose I would be able to prove whether it can really solve any puzzle or not, but, alas, I am not &lt;EM&gt;that&lt;/EM&gt; smart. Let me know what you think, or if you’ve tried this before what your approach was. And if anyone has the recursive, one-statement version I’d love to see it!&lt;/P&gt;
&lt;P&gt;I’m attaching a ZIP with all the code for this solution. If you want to try it out:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Make a database called Sudoku&lt;/LI&gt;
&lt;LI&gt;Run the DB Create.sql script to make the tables&lt;/LI&gt;
&lt;LI&gt;Run one of the provided sample puzzle scripts to load a puzzle (one is an “easy” puzzle, one is an “evil” puzzle)&lt;/LI&gt;
&lt;LI&gt;Run the Viewer.sql script to view the loaded puzzle&lt;/LI&gt;
&lt;LI&gt;Run Solver.sql to solve the puzzle&lt;/LI&gt;
&lt;LI&gt;Run Viewer.sql (perhaps changing the value of SolutionID) to view the solved puzzle&lt;/LI&gt;&lt;/OL&gt;</description></item><item><title>OT: NCAA Pick'em Returns...</title><link>http://sqlblog.com/blogs/rick_heiges/archive/2010/03/14/ot-ncaa-pick-em-returns.aspx</link><pubDate>Sun, 14 Mar 2010 19:28:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:23351</guid><dc:creator>RickHeiges</dc:creator><description>&lt;P&gt;Every year in March, the Men's College Basketball Championship Tourney Begins.&amp;nbsp; For the past few years, I've put together a "League".&amp;nbsp; This year is no different.&amp;nbsp; The prize...&amp;nbsp; Bragging Rights - that's it - nothing else....&lt;/P&gt;
&lt;P&gt;Follow the link below to sign up!&amp;nbsp; Picks must be made by Thursday before the games begin.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://tournament.fantasysports.yahoo.com/t1/register/joinprivategroup_assign_team?GID=65521&amp;amp;P=sqlblog&amp;amp;P=sqlblog"&gt;http://tournament.fantasysports.yahoo.com/t1/register/joinprivategroup_assign_team?GID=65521&amp;amp;P=sqlblog&amp;amp;P=sqlblog&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item><item><title>SQL Saturday #33 in Charlotte!</title><link>http://sqlblog.com/blogs/rick_heiges/archive/2010/02/05/sql-saturday-33-in-charlotte.aspx</link><pubDate>Fri, 05 Feb 2010 16:35:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21877</guid><dc:creator>RickHeiges</dc:creator><description>&lt;P&gt;SQL Saturday is coming to Charlotte next month!&amp;nbsp; If you are in the area, come on by.&amp;nbsp; We have a great line-up of speakers.&amp;nbsp; Find out more information at &lt;A href="http://www.sqlsaturday.com/33/eventhome.aspx"&gt;http://www.sqlsaturday.com/33/eventhome.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item></channel></rss>