Thursday, May 26, 2011

RE: T-SQL Parameters and Variables

Michael Campbell @ sqlmag has an interesting blogpost about parameter and variables. One of the suggestions he has is to declare variables late in T-SQL code (sprocs, functions, scripts, etc). This is something that I've been wondering about and am glad he posted it. From a performance standpoint, late declaration has no effect in SQL since everything is scoped at the batch level, but from a readability standpoint the late declaration is better. If you use an @table then finding the declaration all the way at the top can be cumbersome. Having it close by the first usage makes it easier to locate (and change) the definition if needed.

Check out the rest of the post for some good suggestions including variable naming and default values.

No comments: