September 11, 2007 at 6:14 pm
I created stored procedure with in which sql statements are created like this:
declare @sql varchar(1000)
set @sql = ----(which is created using the inputs from stored proc parameters)
Exec (@SQL)
My question is what is this kind of SQL Statement called as? and is this builded SQL statement not shown when we trace this stored proc? becuase I traced this stored proc but I couldn't get the exact SQL statement what it is executing to find out the duration this SQL statement took.
Also what is dynamic SQL?
Thanks in advance.
September 11, 2007 at 6:25 pm
It's called dynamic SQL... basically it just means that you built your SQL string on the fly and executed it. It has it's up sides (flexibility) and it's down-sides (usually poor performance, security issues, complexity can go crazy pretty quickly).
You should be able to find it if you're running a profiler trace.
September 11, 2007 at 7:02 pm
Won't have poor performance if it's for multi-row batches... on busy systems, most batch procs will probably need to recompile due to data changes in as little as 5 minutes. GUI code is another story... If you use correctly parameterized sp_ExecuteSQL, you won't have recompiles even for single row GUI code but you will be limited to 4k characters (shouldn't be much of a problem for GUI code).
Probably won't have security issues if it's for batches with no GUI interface and the parameters are fed correctly. Lot's of Google hits on how to do it correctly.
And, you can always PRINT @sql if you want to see what it's gonna look like...
The other thing is, unless you are trying to make dynamic column names or dynamic FROM clauses, you probably don't need dynamic SQL at all.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 12, 2007 at 1:03 pm
How to get the execution plan for this kind of stored procs with dynamic SQl in it?
If I want to get the exact execution plan from production environment, do I need to run the stored proc with SHOW EXECUTION PLAN, if so , I am getting error like this object already exists?
Any reply is greatly appreciated.
September 12, 2007 at 2:44 pm
for execution plan you will have to "print" it first to see how it looks like, and then execute it
* Noel
September 12, 2007 at 4:18 pm
"Error like this object already exists?" What on Earth are you doing with the Dynamic SQL?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 12, 2007 at 5:11 pm
Can't I see the execution plan for the stored proc directly?
September 12, 2007 at 7:02 pm
Showing execution plan from general SQL Statement is easy. But how about getting execution plan for a SQL statement that is created from parameter inputs to the stored procedure as I donot know what inputs to give for that parameters.
Can I use the SQL statement that is produced by trace. How to get execution plan for that already build SQL statement i.e., how to get the execution plan with out actually executing the statement?
Thanks in advance.
September 14, 2007 at 7:35 am
I would PRINT the created SQL, copy it from the bottom window in Query Analyzer (QA), paste it in a new QA window then click on the execution plan icon.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply