I haven't really done anything with dynamic SQL yet. However, it looks like you are just declaring a string and tossing your SQL into it. Then using executing the stored procedure sp_executesql (which ...
In this third in a series on working smarter with T-SQL, let's give the built-in sp_executesql stored proc a go. In my previous posts, I've described some methods for generating and executing T-SQL ...
In part 1, we looked at generating T-SQL style code. In part 2, let's get this thing to run! Last time, I showed how a single SELECT statement can be built to create an executable string of T-SQL.