Category Code

text TEXT teXt?

The Problem

A reader ran into a problem on an old post about Automating T-SQL Testing. Thankfully, rather than decide that I’m some useless crank on the internet, this reader took the time to write in to me and tell me about the problem. But, he went one step further: he documented step by step his problem!

The Process

I fired up SSMS and verified his problem (using the thoughtfully provided steps to duplicate). Sure enough, my code didn’t work. Which is odd because I’ve used this very same code to test stored procedures.

At this point, I calmly stood up, walked away from my computer, opened the window, and swore loudly. I then closed the window, walked back to the computer, and sat down. After numerous permutations, I finally searched google in exasperation only to find out it was a capitalization issue (read all about it over here).

Where was this coming from?

The Actual Problem

The GeSHi JavaScript syntax highlighting library was capitalizing the word ‘text’ because it is a datatype in SQL Server. Unfortunately, the text() XML method is lowercase. So, in this isolated incident, my syntax highlighting was causing me problems. The solution was to remove syntax highlighting from that portion of my post and inform the reader of the problem.

What to do?

Well, you need to test things. In this case, though, it was something that I couldn’t effectively test. The developers of the GeSHi plug-in should probably be testing these things (and I’ll be tracking down their info to send them a bug report). The key here is, I guess, to be very very careful. Really I just wanted to share this bug so others don’t get caught by it.

Links for the Week – 2009.08.07

SQL Server

Commenting Your Code Do it, people. I don’t comment enough of my code, but it’s something I keep in the back of my mind all the time.

Fast Running Totals Solution With Ordered CTE I love Mladen’s solution to this particular problem. I usually would use a nasty self join or some kind of hideous cross join to accomplish this.

Teaser: Left Join..the SQL ego crusher Mmmmm SQL brain teaser.

Development

Software is a Wicked Problem

sqlsharp It’s a port of SQLite to C#. I don’t believe source code has been posted yet, but it’s a great idea for portable development.

TeamReview Ask your co-workers for a code review, and get one, all through VSTS. I’m not a fan of meetings. I hate them. I also hate reading your code. I’d be much happier reading your code from my cube. Now I can.

Stuff & Things

How to Set Someone Straight Correcting people is never easy. Use these four simple steps to lead to success.

title unknown

Teach for America founder on the pointlessness of planning, the importance of saying no, etc. This is interesting because it’s a collection of quotes, but the 37signals folks link back to their own articles. Very interesting stuff.

:::WARNING – RANT:::
Netflix Shares Internal Presentation on Company Culture I was going to try to build an entire blog post out of this. Then I realized it would sound like I rant, so I decided to ran in my weekly link dump instead because it’s my blog and youcantmakemegotobedearlyanymore! So… yeah, anyway. Netflix sounds like an amazing place to work – they do everything they can to let good employees innovate and reward them effectively. For people outside of the consulting world, it doesn’t make sense to hold them to some weird standard of X days of holiday a year. Most of our work is based around meeting goals, not producing sprockets. Does it really matter if I want to take an extra few days off here and there? If I’m a top producer, that should be fine.

The other thing that really struck me about Netflix is how they compensate their employees. They hit a few things on the head that I’ve always agreed with – don’t bonus your employees, pay them. If you tell me, at hiring, that you’ll give me a $5,000 bonus in 6 months, pay me $5,000 more now. I don’t want a bonus. A bonus won’t make the payments on my sweet ride. They also pay their employees what they think they’re worth. Salary negotiations are a game. Playing games with employees that you allegedly trust and who are supposed to trust you is not a good way to conduct business.

Many businesses would do well from reading this slide deck and learning from it.
:::END RANT:::

Links for the Week of 2009.07.31

SQL Server

Fast Running Totals solution with ordered CTE update? Who says you need a cursor to get a running total? I’ve typically done these kinds of things with expensive CROSS JOINs and/or nasty INNER JOINs. Good to know there’s a nicer way.

Extended Events Manager Update: Now A SSMS Addin SQL 2008 Extended Events are a powerful new way of troubleshooting problems with SQL Server. One short coming is that there is no UI support for this new feature. (From codeplex)

Development

Think First, Doing Is For Later A non-TDD approach to solving programming problems.

Branch Per Feature Source Control Part 2: How (Theory)

Basecamp, now with more vroom Ever wonder how people go about testing applications to see where there hardware starts to fall apart? Mark over at 37signals did just that and blogged about how they got some impressive performance gains fairly quickly.

Stuff & Things

404 error: building not found

Tom Hanks trash can

Links for the Week of 2009.07.24

SQL Server

Learn Microsoft BI So you want to learn all about the BI, do ya? Lots of videos. Lots and lots of videos. They’re all free.

Getting a NEWSEQUENTIALID() How do you get the ID that’s created after an insert if you’re using NEWID() or NEWSEQUENTIALID()? Pat Wright has the answer! (Hint: The answer is not “a taco truck”.)

Development

Nobody Hates Software More Than Software Developers I couldn’t agree with Jeff more. Most software is atrocious. I recently bought a new printer. I wanted to get duplexing to work. I searched for the feature for hours before sighing and decided that it must be hidden in a secret driver on the CD that came with the printer. After putting the CD in the drive, the installer opened, was ugly, ran for five minutes, and promptly crashed while citing that it did not actually know the reason for the crash. The duplexing feature was later found in a secretly control panel option of the generic driver. The point is: software sucks.

What’s new in NHibernate 2.1 Did you know that NHibernate 2.1 is out? Did you know that it has new features? Did you know that Andrei Volkov is using my old blog theme?

Stuff & Things

BRIBERY, THREATS, BROKEN-DOWN VEHICLES, LAWSUITS, PIONEERS, GOOD FOOD: TALES FROM OC’S TACO TRUCKS I love taco trucks, loncheras, whatever you want to call them. The food is hot, cheap, and amazing. Reading the story behind some of them was pretty cool, too.

Columbus Taco Trucks IN the same vein, here’s a blog that maps and reviews all of the taco trucks in the greater Columbus area.

PingWire is a near-live feed of images being posted to twitpic, yfrog, and twitgoo. It’s pretty cool.

From Tables to Objects – Follow Up

First off, I want to say thank you to the Central Ohio .NET Developers Group for giving me the opportunity to speak last night. I really enjoyed it and I was glad that I could share some of the things that I’ve learned with such a great group of people.

Everyone at CONDG was very welcoming, as they always have been, and the questions from the audience were fantastic. It’s always good to hear that I’m not the only one who has run into some of the scenarios I mentioned in the presentation, and it’s also always great to hear real life success stories from developers who have made use of the techniques that I’m speaking about – in this case NHibernate.

I have to take this opportunity, as well, to think my host, admin, and brother who took time out of his evening to make sure that the presentation resources automatically posted on time through WordPress. Unfortunately, they didn’t post on time and he looked into to the problem to make sure that the resources would be there by the time the presentation was done. So a big thanks to Mike for keeping things running and going the extra mile to make sure the resources were available for everyone to download when the presentation was over.

97 people were able to make it to the presentation. If you weren’t able to attend and would like to see this presentation, let me know. If there’s enough support I can either schedule a LiveMeeting to do this presentation again one evening, or I can record it and make it available online for viewing. Let me know in the comments or send me an email and let me know.

From Tables to Objects: Resources

Here are the slides and sample code from my recent presentation “From Tables to Objects” that I gave at the Central Ohio .NET Developer Group.

To those who did not attend, you can skip the rest of this post unless you want some NHibernate and SQL Server resources.

NHForge – this is the project home page for NHibernate.
Fluent NHibernate – download Fluent NHibernate.
NHProf – NHibernate Profiler
NH Contrib – Additional, non-core, NHibernate libraries, includes NHibernate.Linq

What’s new in NHibernate 2.1

Using Sparse Columns

If you were at CONDG and would like to rate my talk, please go to SpeakerRate and let me know what you thought. I’d love to hear your open, honest feedback so I can improve the information and experience I share with you.

To create the sample database for this project, create a copy of the AdventureWorks database and rename it to ftto. The rest of the data and tables created are in the ftto.sql script. It’s just that simple.

From Tables to Objects Source Code
From Tables to Objects PowerPoint

So Much Slacking, So Little Time

I’ve been slacking on the blogging front. Which is a shame, because I’ve been busy doing some really fun things recently. Want to know about one of them? I hope you do, because I’m going to talk about it regardless.

I spent last weekend at the Columbus Give Camp. What was the point of this? Well, the point of Give Camp is for a bunch of geeks to get together and help out various charities.

Over the course of the weekend, I worked on the website for HopeMongers. A lot of the underlying work had been completed, but the public facing portion of the site needed some style and panache to bring it inline with the expectations of Sam Henry, the man behind HopeMongers.

There are a few great things that come out of a weekend like this one.

  • You get a chance to provide much needed help to a charity. Would the work on HopeMongers have been completed if we weren’t involved? Probably, but it would have taken a lot longer.
  • You get to work with a lot of talented people, while you help out a charity. If I were doing this alone, I might not have had as much motivation – I won’t lie about that. But working with other people is great. Especially when you’re all in the same room, sitting around a single table, writing code until the small hours of the morning (or if you’re like me, late hours of the evening). There’s a great collaborative, startup, feel to the whole thing. Ideas bounce around. People help each other solve problems. There’s so much collaboration.
  • You learn a lot about yourself. I learned that I really liked working with Sam on the HopeMongers website. Which in turn made me realize that what I really liked was helping out.

Thanks to everyone involved in the Columbus Give Camp for putting things together and letting me help out by building a database schema for the back end of the site.

A big thanks to Steve Andrews, Chris Funk, Kevin Kuebler, Marwan Saleh, Phil Japiske, Kevin Malone, Carey Payette, and anyone else who worked on the HopeMongers project that I left out. You guys made it a great weekend and I’m glad I had the opportunity to work with all of you.

And a special thanks to Sam Henry for the wonderful project, the great experience, and the opportunity to keep giving back.

Links for the Week of 2009.07.17

SQL Server

File/Folder/Share Permissions for DBAs and Database Developers So you thought you knew everything you needed to know about security? Take a look and make sure your assumptions were correct.

Groovy Baby, Yeah Tony Bain hits up the sweet spots from Groovy Corp’s press release for a new relational database platform. New DB?!? That’s right a new, in memory, RDBMS.

Development

The Law of Demeter Is Not A Dot Counting Exercise Phil Haack gives the best explanation I have ever read for the Law of Demeter, which is oft quoted by developers and oft misunderstood by me.

In Branch Per Feature Source Control Introduction and Part 1: Why? Derick Bailey is going over a great idea to isolate source code within source control. Rather than branching for each release, branch for each feature. Hit up the articles for an in depth look at this idea.

Stuff & Things

Insanely Twisted Shadow Planet New Trailer Michel Gagne is an illustrator who is working on what looks like a phenomenally beautiful video game. Check out the trailer for some glory.

A Beautiful Method to Find Peace of Mind

Crash Bonsai Thanks to William Gibson I became aware of this art.

Links for the Week of 2009.07.10

SQL Server

Object Naming Standards Scott Herbert put up an overview of the naming standards that he uses. Like the obnoxious lout that I am, I commented. The point is, of course, to use naming standards. Take a look at Scott’s for an example.

No SQL There’s a big hullabaloo going on about this whole “No SQL” movement. Here’s Andrew Fryer’s take on it.

Performance Counter for SQL Server Ever want to know what those performance counters actually mean?

Development

Thrive for Developers is Online Brian Prince, a great guy and former co-worker, highlights some of the benefits of the newly launched Thrive for Developers program. Check it out for some more info on how to improve your career and how MS can help you do it!

10 Useful Firefox Extensions to Supercharge Firebug Firebug is the best tool for web development that I have found so far. These extensions make it even better.

Stuff & Things

Anticipate Your Interviewer’s Next Question to Ace Your Job Interview Good interviewing advice can be found here.

Day made, mind blown: it’s Portal in ASCII

Oh No You Didn’t! Everyone needs a chair like this.

Counting Children with CTEs

Have you ever wanted to get a running total of all of the descendants of each tree node? This sort of thing is useful, especially if you don’t want to pull back an entire object graph just to compute the count of a child collection.

IF OBJECT_ID('tempdb..#categories') IS NOT NULL
  DROP TABLE #categories;

IF OBJECT_ID('tempdb..#product_categories') IS NOT NULL
  DROP TABLE #product_categories;

CREATE TABLE #categories (CategoryId INT, ParentCategoryId INT);
CREATE TABLE #product_categories (CategoryId INT, ProductId INT);

INSERT INTO #categories VALUES (1, NULL);
INSERT INTO #categories VALUES (2, 1);
INSERT INTO #categories VALUES (3, 1);
INSERT INTO #categories VALUES (4, 2);
INSERT INTO #categories VALUES (5, 3);
INSERT INTO #categories VALUES (6, 2);

INSERT INTO #product_categories VALUES (1, 1);
INSERT INTO #product_categories VALUES (2, 15);
INSERT INTO #product_categories VALUES (2, 18);
INSERT INTO #product_categories VALUES (3, 12);
INSERT INTO #product_categories VALUES (6, 34);
INSERT INTO #product_categories VALUES (4, 35);
INSERT INTO #product_categories VALUES (5, 99);
INSERT INTO #product_categories VALUES (3, 43);
INSERT INTO #product_categories VALUES (6, 54);
INSERT INTO #product_categories VALUES (3, 92);
INSERT INTO #product_categories VALUES (2, 77);
INSERT INTO #product_categories VALUES (5, 62);
INSERT INTO #product_categories VALUES (4, 42);
INSERT INTO #product_categories VALUES (1, 11);

;
WITH  cte(CategoryId, ParentCategoryId, c)
        AS (SELECT  c1.CategoryId,
                    c1.ParentCategoryId,
                    (SELECT COUNT(*)
                     FROM   (SELECT DISTINCT
                                    ProductId
                             FROM   #product_categories AS pc
                             WHERE  pc.CategoryId = c1.CategoryId
                            ) AS t1
                    ) AS c
            FROM    #categories AS c1
            UNION ALL
            SELECT  c2.CategoryId,
                    c2.ParentCategoryID,
                    d.c
            FROM    #categories c2
                    INNER JOIN cte d ON c2.CategoryId = d.ParentCategoryId
           )
  SELECT  cte.CategoryId,
          cte.ParentCategoryId,
          SUM(c) AS ProductCount
  FROM    cte
  GROUP BY cte.CategoryId,
          cte.ParentCategoryId;

So, how does mess work? Carefully.

The innermost select, t1, builds a virtual table that is nothing more than the product count under a single Category. There is a DISTINCT on here just to make sure that there are no duplicates from the #product_categories table. Theoretically this should be a group by, but this was a super fast hotfix that needed to go into production ASAP. Mistakes happen.

Moving outwards we encounter a SELECT COUNT on t1. This is where we get the actual count of products under the current category. Pretty standard.

What’s interesting, to me at least, is that in the recursive portion of the CTE, we’re working our way UP the hierarchy, from the bottom. This makes sure that when we do a sum of c, grouped by category and parent category, we’ll have the proper count in our CTE results.

What’s the advantage of this approach? You don’t have nasty deeply nested sub-selects. You can use this to populate a ChildCount column in your table as a result of a trigger. This is also, usually, fast enough to run live or else to use as a materialized view.

This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 401 items have been purified.