"Yes, I'm still going out as team captain for coin flips, and yes I'm calling it correctly almost 50% of the time!"
-- Drew Bledsoe

Hip Hop Coding

I’ve never used Dynamic SQL before yesterday.

If you used to, still do, or think you will use it, the article The Curse and Blessings of Dynamic SQL seems to be an excellent write-up on some important aspects. Including defending against dreaded SQL injection attacks.

On help forums where someone responds “Use Dynamic SQL!”, there’s a good chance there will an example like the following:


DECLARE @SQL nvarchar(2000)
SET @SQL = 'SELECT * FROM TableName WHERE Server = ''' + @ServerID + ''''
EXEC (@SQL)

However, simply using EXEC is not a very good practice. Instead sp_executesql can be used to allow a more secure parameterized query:


DECLARE @SQL nvarchar(2000)
SET @SQL = 'SELECT * FROM TableName WHERE Server = @ServerID'
EXEC sp_executesql @sql, N'@ServerID nvarchar(100)', @TableName

For some reason, the syntax created a mental image of a du-rag clad Darius N’ServerID rapping about the virtues of Dynamic SQL.

October 20th, 2009 5 comments
Posted by Donnie Filed under Computer-fu

  1. Derek Slater posted the following on 23 October 2009 at 2:07 pm.

    You’ve seen this:
    A SQL query goes into a bar, walks up to two tables and asks, “Can I join you?”

    Unrelated: So on Tuesday I walk into the club and a guy (okay Harvey actually) looks up at me and says, with no preamble: “Who IS Liquid Egg Product?”

        Reply to Derek Slater
  2. cottagesweet posted the following on 23 October 2009 at 9:16 pm.

    You may as well have posted in a Japanese and French mix for all I could understand this.

        Reply to cottagesweet
  3. Donnie posted the following on 25 October 2009 at 2:57 pm.

    @Derek: What a horrible joke. I couldn’t stop laughing for about 15 minutes.

    Re: Harvey. LOL! How did you answer?

    @Mom: Don’t worry, it’s not only you. It doesn’t make much sense to most other readers either.

        Reply to Donnie
  4. Derek Slater posted the following on 27 October 2009 at 11:36 am.

    “Beats me” :)

    Actually the longer you make your answer, the more bizarre it gets:

    “Some guy named Donnie Briggs – in Houston I think, or Miami – a programmer I believe – who has this blog with some alter-egos that are like the egg version of popular characters – and we, um, kinda think the same stupid things are funny… [long pause] I guess I don’t really have any idea.”

        Reply to Derek Slater
  5. The Mascot posted the following on 27 October 2009 at 4:57 pm.

    What? He didn’t ask for me by name?

        Reply to The Mascot

Leave a reply