Rodney

When When Situation

Published Wednesday, February 28, 2007 11:08 PM

I have been pondering best and worst SQL practices lately. When I was a younger man, I had a mentor tell me one time, specifically about a COBOL program he was writing, "As long as it works and works quickly...who gives a rat's ass?" I have kept that with me, but have not always adhered to the logic because like others I eschew common bad practices in SQL. DBA's know, but of course do not always obey the guidelines of others who have come to believe that there are certain things that should never be done. Here is a quick list of wrist slaps.

Cursors.....slap...ouch

Temp Tables....slap....WHY NOT...ouch

dynamic SQL...(Set @whynot = 'select horrifying_SQL_injection_attack from #mytable where @InputInjectionHere = SemiColon; Exec @whynot)

xp_cmdshell  (NEVER)

We know what not to do and we develop skills to thwart these evil thoughts when temptation sets in. 

So..should I spend the rest of my working life avoiding bad practices when at times they are the quickest and easisest solutions--one-off answers to questions that DBA's get asked all the time? I guess the answer would be yes, avoid it because you are smart and know better. But temp tation...like temp  tables is difficult to avoid.

So over the next few blogs, I will jot down all of the bad, bad, bad things I do everyday. This will be my tell all, so others can gasp at my egregiousness. 

I feel shamed already.

Select Shame INTO #MyTemporaryShame where OverItAlready = "Yes" 

by Rodney

Comments

 

Phil Factor said:

I find  an sudden surge of excitement  when using such things as cursors, xp_cmdshell, temp tables, Select into, sp_oacreate, and such naughtiness. It is the thought of the irritation it causes to the SQL Server pundits who like to dictate what is used and when without explaining precisely WHY such things are wrong, or testing out their dictats. (but believe me, some are wrong, in certain circumstances!)
March 1, 2007 5:25 AM
 

Louis Davidson said:

To me, as long as your mind *first* tries to work out the best way to solve a problem, there is nothing that is available that shouldn't EVER be used.  So I first try (very hard) to solve in a single query, using derived tables, CTEs, subqueries, etc
Temp tables can be a fallback in some cases, if the query is complex and the optimizer doesn't quite optimize the query as desired.  If you feel a loop coming on, I consider using user defined functions to encapsulate the stuff I might do in a loop.  And sometimes I do dynamic SQL to cover searches with large variances in parameters.

The problem that I see is, the fallbacks of the professional relational programmer are the crutches of the novice and too often the only thing the newbie actually knows about because it feels like the functional programming that they already know.  
March 1, 2007 9:35 AM
You need to sign in to comment on this blog

















<February 2007>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910
Larry Gonick: Geek of the Week
 Cartoonist, mathematician, historian and environmentalist. Larry Gonick proved that learning could be... Read more...

A SysAdmin's Guide to Change Management
 In the first in a series of monthly articles, ‘Confessions of a Sys Admin’, Matt describes the issues... Read more...

Exchange: Recovery Storage Groups
 It can happen at any time: You get a request, as Admin, from your company, to provide the contents of... Read more...

Build Your Own Virtualized Test Lab
 Desmond Lee explains the fundamentals of building a fully functional test lab for Windows Servers and... Read more...

Rendering Hierarchical Data with the Treeview
 It sometimes happens that Web Server controls that visualize data don't quite fit with the way that... Read more...