- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
select first from comments;
Admin
What The Fuuuuuhhhggggg
Admin
For those of us who don't speak sql, what the Fuuuuuhhhggggg is wrong with this?
Admin
It's multiple select statements when a single select will work just as well, and without all the declared variables, too.
Admin
The TSQL function ISNULL(@variable,'') returns '' if @variable is null.
You could also argue that it's the front-end/mid-layer's job to deal with nulls.
Admin
Also note that isnull is not index-friendly, so it is probably a bit slower than it should be.
Admin
eeek, I meant
The TSQL function ISNULL(YourFieldName,'') returns '' if YourFieldName is null.
Admin
select @w = w from wtf where id = @id select @t = t from wtf where id = @id select @f = f from wtf where id = @id
select @w + @t + @f
Admin
I was going to go all witty with this, but there are too many WTFs associated with this.
Admin
Yes, but then any non-developer who looks at the code thinks that writing code is easy.
BTW anyone who names tables "Sitr0110" should be shot.
CAPTCHA: ********
Admin
A pre-scripted phone banking session could get that long... Though I can't think of a legal reason to store that in a database with people's names and addresses...
Admin
There is actually no non-trivial function that is "index-friendly" if used as part of a WHERE clause - only the columns themselves are indexed, and if you chuck it into any function, the optimizer has to scan the whole index. But applying a function to the query's output has almost no effect on performance unless the function itself is extremely slow, which ISNULL definitely isn't.
This entire hunk of junk could have been written as one SELECT statement with a bunch of ISNULLs, and would run in about 1/50th of the time. Of course, the fact that nulls are being turned into empty strings suggests an even worse WTF somewhere in the code, unless this is being directly used to feed a report (and nothing else). And then you have all the varchar(255) columns, but that's the least of our worries here...
Admin
To put it in simpler terms, let's say you want to buy a dozen donuts. You tell the clerk which donuts you want; he takes each donut from the case and places it in a separate bag. Once he's got all twelve bags lined up on the counter in front of him, each with a single donut in it, he opens each bag in turn and puts the donut into a dozen box. Once all twelve donuts are in the box, he closes it and hands it to you.
Then his co-worker, noticing your puzzled expression, comes along and explains that it's better to do it this way in case you change your mind about one of the donuts and he has to swap it out. And you look at him, and the clerk, and realize the two of them have been doing crack all morning. The end.
Admin
Admin
Admin
Um, where to start? Let's see, there's this mess
which does absolutely nothing.But the major WTF is that this whole page long crapfest could be written as:
Admin
I always wrap my condiational code in BEGIN and END in SQL (and other where you only need it for multi-liners), even if it's a one liner.
There is no performance issue and it means you are using the same convention for all condition statements.
Not to mention the cut and paste issues I've seen with people who don't.
Admin
I agree to a certain extent, though I'd argue that the application should deal with nulls.
Either way: W? T? F?
Admin
You're missing the subtle difference between 0110 and 0100. Any interaction between those tables must be pure joy to work with.
Admin
I like donuts... and crack...
Admin
Admin
Admin
It's rather like your partner giving you a shopping list and instead of driving to the shop, buying everything on the list and returning home, you instead drive to the shop, buy the first item, drive home, drop it off, drive back to the shop, buy the second item, drive home, etc.
If you're using SQL you should aim to retrieve all the values you need in one statement. It helps if you understand that the database has to work pretty hard to find any particular row, but virtually no extra effort to retrieve additional values associated with that row.
This particular query could have been rewritten as:
(ordering by the ID in reverse and taking the top result is the same as selecting MAX(Sitr0100Id))
Admin
Admin
And why do all the variables start @ln, or is it @In?
That's dafter than tables starting tbl.
Admin
It's standard practice to name SQL variables beginning with @.
Admin
$sp=' '; $comment1='this'.$sp; $comment2='way'.$sp; $comment3='you'.$sp; $comment4='can'.$sp; $comment5='swap'.$sp; $comment6='words'.$sp; $comment7='around'; $comment=$comment1.$comment2.$comment3.$comment4.$comment5.$comment6.$comment7.'.'; echo $comment;
Admin
Admin
Admin
Admin
I originally thought it could be some sort of Hungarian Notation (some find that to be a bad practice too), but there's no rhyme or reason to the prefix.
As for the isNull function, the code base that is calling the SQL statement should be handling those nulls. It's one thing if you have to concatenate values in the query (concatenating a string and a null in SQL yields a null), but that's not the case here.
Admin
Although you have to admit an overuse of "In" and "ln" is WTFery of the highest order.
Admin
Admin
It's more like the clerk takes a bag, goes to the donuts, places one in the bag, and then puts the bag in a table in back. He then gets another bag, goes to the donuts, places one in the bag, and then puts the bag in a table in back. Repeat.
(ie: Your version could be done by taking twelve bags and placing one donut in each, whereas the entire "take a bag, put a donut in it, and put the bag in a temporary storage area" needs to be completely done, from start to finish, twelve separate times.)
Admin
I don't know if this is better or worse than:
Admin
Admin
Admin
Admin
Fail!
In output yes, but in performance NO! Max() / Min() is O(n) whereas sort (ORDER) is at best O(n log(n))
For small n it doesn't matter, but for large n.....
In this particular case it likely doesn't matter (as the application will likely be slow as molasses anyways considering that SQL) :-)
Yours Yazeran.
Plan: To go to Mars one day with a hammer.
Admin
Almost correct: if there are no rows where Sitr0100Id = @lnSitr0100Id, then your select will return an empty resultset. If there are multiple rows with the specified condition, then your select will return multiple rows. The original code will always return 1 row.
Of course, it make sense for the caller to deal with the non-existing row as an empty resultset, but your code is not 100% compatible with the original code.
Admin
Okay! Okay! I'll change the name of the table to "babysitters_born_jan_10"! Don't shoot! Please!
Admin
Isnull doesn't matter a whole lot unless it's used in the WHERE clause. In the SELECT clause, it only adds a "compute scalar" to the query plan.
But still... this is why we need to actually test people who SAY they know SQL... because usually, they don't. In an interview I was given once, I was told I was the only person who ever passed their SQL test, and it was ridiculously easy, but I wasn't surprised that nobody did well on it because they were looking for C# programmers, who also "know SQL"
Admin
Meh. Our production db is full of "boolean" fields that are VARCHAR2(255). Not only that, we have one table (incrementally modified over the years by different developers) with four VARCHAR2(255) fields constrained to:
I'm not kidding. This isn't written for effect.
As for me, I insist on using NUMBER(0, 1) for my booleans. Sure, it's another version, but at least it's right.
Admin
If you were searching through a flat file yes, but in a database NO!
Max(), Min(), and SELECT TOP 1 FROM ... ORDER BY are all O(1) in a table indexed by the field in question -- which this table certainly should be, given this usage.
Admin
The best part is that the coworker had no problem with it.
Admin
DOUBLE WIN
Admin
And the nominee for The New Real WTF is...
(You don't think the first four people all thought "at least MINE is right"?)
Admin
Why would you use a decimal?
Wouldn't it make more sense to use a NUMBER(1)?
Admin
Ow great! now i can use SELECT * FROM babysitters_born_jan_10 in stead of:
SELECT h.* FROM HUMANS INNER JOIN JobDescriptions j on j.ID = h.JobDescriptionID WHERE j.Description = 'Babysitter' AND DATEPART (m, h.BirthDay) = 1 AND DATEPART (d, h.BirthDay) = 10
which allows for much faster coding :) and a lot of tables yeah
Admin