I am building and executing some dynamic sql and wanted to use SQL Server profiler to view that execution statement. Do you know what filters /settings I have to use in order to see that? I have looked through them and could be just looking right over the obvious setting.
EDIT:
The dynamic sql is called within a procedure.
Thanks in Advance,
--S
Following your edit you would need either the SP:StmtCompleted or SP:StmtStarting events (You can find these in the "Stored Procedures" section).
This might collect quite a lot of data so apply appropriate filters.
Take a look at the SQL:BatchCompleted event in the TSQL category and make sure you have the TextData column checked/shown. The Standard (default) template includes these already.
Related
I need to get each statement of given SQL Server stored procedure.
To achieve this, I generate execution plan XML and look for "StmtSimple" nodes in query.
This approach is quite slow for large procedures.
Is there any way that I can get every single statement of the procedure without generating XML execution plan?
When you have the text of a stored procedure, you can perform a full parse on it using the TransactSql ScriptDom. If you just want to get each statement, it's possible to do that using this method.
However, I'm not convinced that it would actually perform much better than using the method you are using now. It's just another option to try.
Edit -> The ScriptDom is part of the SQL Server 2012 Feature Pack, and is contained in the 'SqlDom.msi' install.
When I run an SQL trace I want to see the TSQL statements that are executed. I chose all the TSQL events to show. For most like "SQL:StmtStarting" I can see the TSQL but for "Exec prepared SQL" the TextData is blank.
Any ideas? I want to see the query that will be executed.
TIA
SQL:StmtStarting should give you the text of your TSQL every time. You should not even need any of the other options if all you want is to see the TSQL that will be executed? Are you saying that you are running TSQL queries and not seeing them with this option?
I have hunted and hunted for a way to handle this without any luck. The only way I've found is to find the earlier sp_prepare statement in the trace.
I wrote a utility to summarize traces files (ClearTrace). It handles matching sp_prepare statements to the later executions of those statements where they don't cross trace file boundries.
I am troubleshooting an application that uses a SQL Server database and I am seeing a lot of sp_execute calls.
I can not seem to find the sp_prepare calls.
How can you inspect all of the prepared SQL statements in memory?
I was looking for a way to see the actual SQL statements executed by sp_execute in SQL Server 2008 R2 Profiler.
To do this, I created a new trace, and clicked on the "Events Selection" tab. I selected "Show all events" and checked Stored Procedures > SP:StmtCompleted. Running the trace, I was then able to see the actual SQL statements.
I ran into this issue as well. SQL Profiler was not capturing the sp_prepare statement because it occurred before the SQL Profiler trace had started to run. The various postings that rely on sys.dm_exec_sql_text did not help because I could not find the correct sql_handle or plan_handle value to provide for that stored procedure.
I found a solution from this blog post: in SQL Profiler, click the "Show all events" checkbox and then under the "Stored Procedures" heading choose "SP:CacheHit".
In the resulting SQL Profiler output, you'll see an "SP:CacheHit" row containing the cached SQL statement near your "RPC:Starting ... sp_execute" statement.
You can then reconstruct and reexecute the full SQL statement in SSMS if you wish using:
exec sp_executesql #stmt=N'{statement from SP:CacheHit}',
#params=N'{parameter declaration from SP:CacheHit}',
#param1={value}, {...parameters from RPC:Starting sp_execute statement}
Following up on my comment above, I found a number of relevant links:
How can I find out what command sp_execute is running (without using Profiler)
SP_EXECUTE executing... what?
See the query in sp_execute
Microsoft has documentation but it maybe a challenging piecing things together (as always). If the plan handle is known, you can use this:
sys.dm_exec_sql_text (Transact-SQL)
This is a table-valued function. You can see a blog article here that exploits such table-valued functions to retrieve object dependencies for a valid handle of a compiled (prepared) plan.
The title pretty much says it all.
How can I view the original SQL that created a stored procedure in SQL Server 2008?
Is this possible? I've been searching online for some leads, but I'm either missing correct vernacular or I'm just looking for something that can be found by some other means.
My basic problem is that I've got a SQL Server 2008 db here with a couple hundred stored procedures and I want to see what they are doing. I need to copy one and modify it slightly and then use it.
Open up management studio and expand the database you are after. Inside of there is a programmability folder, expand that and you will see the stored procedures. Right click on one of them and select modify.
From a query window on the db you can execute sp_helptext YOURPROCEDURENAME It's a shorthand for what Martin described.
To get the definition
select object_definition(object_id('sp_help'))
Or in management studio right click the procedure and choose a scripting option.
As long as it was not encrypted sp_helptext is the stored procured you want to show the text of any stored procedure
Of course if you were storing your sps in your source control as you should be doing, you would go there and look at it and even be able to see previous versions.
For any of the answers given so far, if there was any set up done - to create a #temp table that the proc depends on, for example - that won't exist in the results because SS stores the functional code for the proc definition, not all of the SQL used in the creation. Some things you might have to infer.
What is a dynamic SQL query, and when would I want to use one? I'm using SQL Server 2005.
Here's a few articles:
Introduction to Dynamic SQL
Dynamic SQL Beginner's Guide
From Introduction to Dynamic SQL:
Dynamic SQL is a term used to mean SQL code that is generated programatically (in part or fully) by your program before it is executed. As a result it is a very flexible and powerful tool. You can use dynamic SQL to accomplish tasks such as adding where clauses to a search based on what fields are filled out on a form or to create tables with varying names.
Dynamic SQL is SQL generated by the calling program. This can be through an ORM tool, or ad-hoc by concatenating strings. Non-dynamic SQL would be something like a stored procedure, where the SQL to be executed is predefined. Not all DBA's will let you run dynamic SQL against their database due to security concerns.
A dynamic SQL query is one that is built as the program is running as opposed to a query that is already (hard-) coded at compile time.
The program in question might be running either on the client or application server (debatable if you'd still call it 'dynamic') or within the database server.