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.
Related
I use an application connected with an sql database. I found using the profiler that the application runs an update query with a syntax error. I don't have access to the application's source code. The result is that the record is not updated. Is there a way to modify the query every time it is executed with something like trigger? I can't use INSTEAD OF because there ism't any record updated or inserted.
This answer
https://stackoverflow.com/a/3319031/1359088
suggests a way to log to a text file all the errors. You could write a little utility and schedule it to run every hour or whatever, which could read through this log, find the erroneous sql statements, fix them, then run them itself.
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.
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.
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.
When the SQL Server (2000/2005/2008) is running sluggish, what is the first command that you run to see where the problem is?
The purpose of this question is that, when all answers are compiled, other users can benefit by running your command of choice to segregate where the problem might be.
There are other troubleshooting posts regarding SQL Server performance but they can be useful only for specific cases.
If you roll out and run your own custom SQL script,
then would you let others know what
the purpose of the script is
it returns (return value)
to do to figure out where problem is
If you could provide source for the script, please post it.
In my case,
sp_lock
I run to figure out if there are any locks (purpose) to return SQL server lock information. Since result set displays object IDs (thus not so human readable), I would usually skim through result to see if there are abnormally many locks.
Feel free to update tags
Why run a single query when a picture is worth a thousand words!
I prefer to run the freely avaialable Performance Dashboard Reports.
They provide a complete snapshot overview of your servers performance in seconds. You can then choose the a specific area to investigate (locking, currently running queries, wait requests etc.) simply by clicking the apporpriate area on the Dashboard.
http://www.microsoft.com/downloads/details.aspx?FamilyId=1d3a4a0d-7e0c-4730-8204-e419218c1efc&displaylang=en
One slight caveat, I beleive these are only available in SQL 2005 and above.
sp_who
http://msdn.microsoft.com/en-us/library/aa260384(SQL.80).aspx
I want to see "who", what machines/users are running what queries, length of time, etc. I can also easily scan for blocks.
If something is blocking a bunch of other transactions I can use the spid to issue a kill command if necessary.
sp_who_3 - Provides a lot of information available elsewhere but in one nice output. Also has several parameters to allow customized output.
A custom query which combines what you would expect in sp_who with DBCC INPUTBUFFER(spid) to get the last query text on each spid ordered by the blocked/blocking graph.
Process data is avaliable via master..sysprocesses.
sp_who3 returns standand sp_who2 output, until you specify a specific spid, then gives 6 different recordsets about that spid including locks, blocks, what it's currently doing, the T/SQL it's running, and the statement within the T/SQL that is currently running.
Ian Stirk has a great script I like to use as detailed in this article: http://msdn2.microsoft.com/en-ca/magazine/cc135978.aspx
In particular, I like the missing indexes one:
SELECT
DatabaseName = DB_NAME(database_id)
,[Number Indexes Missing] = count(*)
FROM sys.dm_db_missing_index_details
GROUP BY DB_NAME(database_id)
ORDER BY 2 DESC;
DBCC OPENTRAN to see what the oldest active transaction is
Displays information about the oldest
active transaction and the oldest
distributed and nondistributed
replicated transactions, if any,
within the specified database. Results
are displayed only if there is an
active transaction or if the database
contains replication information. An
informational message is displayed if
there are no active transactions.
followed by sp_who2
I use queries like those:
Number of open/active connections in ms sql server 2005