• While I agree with everything that Jeff and Gail said, I wanted to add to a couple of these:

    clive (10/31/2008)


    What is the preferred option, if exists or if @@rowcount.

    If @@ROWCOUNT is already set then you should use it. Otherwise, use EXISTS() as Gail pointed out.

    What is the preferred option, temporary table or derived table.

    The general thinking is to prefer derived tables because they leave the optimizer with more freedom to optimize. However, experience also shows that the more complex the query, the less likely the optimizer is going to make the best choices. Consequently, for significantly complex queries, converting one or more derived tables to temporary tables serves both as a way to force certain decisions on the optimizer and as a way to simplify the query, thus allowing the optimizer to make better decisions with what remains. The catch here is that if you convert the wrong derived table to a temporary table, then you end up forcing a bad choice on the optimizer, thus making the overall performance worse instead of better.

    What is the preferred option, static or dynamic query.

    Static is preferred because it is safer, easier to read and write and (marginally) faster. However, some parametrized queries cannot be done as static SQL and many more can only be done in a terribly inefficient way. In these cases you should use dynamic SQL instead, but with great caution to avoid the possibility of Injection.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]