View SQL prepared with sp_prepare - sql

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.

Related

SSIS SQL Executation Task error: unable to run some sql queries

I'm working to make some fact tables (taking some data from some resources, doing some transformations and putting them in a table). My main dilemma is that I can't run any SQL query other than select, update, and insertion. As soon as i try:
exec someProcedure
or a conditional statement (if #part1 ...) or even (create table ...) I take errors. Opening the task to build my SQL statements and find problems it gives errors ranging from (The Set SQL construct or statement is not supported.) to (The EXEC SQL construct or statement is not supported.).
I looked for numerous topics here on stackoverflow but none were actually addressing me problem.
Thanks,
You can see a view of what I'm facing in this picture :
I expect to run my SQL commands as usual in SSIS.
Try changing the SQL Source Type from Direct Input to Stored Procedure and just specify the stored procedure name instead of Exec stored procedure
Also make sure that you have selected the relevant TargetServerVersion from the project configuration:
How to change TargetServerVersion of my SSIS Project
Based on your comments, you are using SQL Server 2012 with Visual Studio 2010 which are not compatible.
You have to use Visual Studio 2012 or 2015+ (backward compatibility added). You can refer to the SSIS tag wiki for more info:
https://stackoverflow.com/tags/ssis/info

SQL Server Profiler - Textdata On 'Prepare SQL' and 'EXEC Prepared SQL' Commands Blank

I have inherited an application that uses SQL Server as it's database.
To understand the tables accessed from the application when doing specific tasks in the closed code application I usually use SQL Profiler to identify the table and queries performed behind the scenes for each function ( for example adding a customer ).
However with this application all the SQL statements issued are 'Prepare SQL' and 'Exec Prepared SQL' both of which on this application return nothing in the 'TextData' column which usually shows the SQL commands performed.
The login events are also not shown and all SQL statements are shown in profiler attributable to the applications service account. I have therefore deduced users sign on to the application but the application communicates with the database via the service account and the SQL statements are hidden somehow.
Does anybody know how to expose/reveal the TextData column contents on the 'Prepare SQl' and 'Exec Prepared SQL' statements so I can see which tables the application is using? I know there are lots of packages out there that claim they can do this but I suspect they are just using the same information as profiler so won't show anything extra.
Select the SQL:StmtStarting under TSQL and SP:stmtStarting under Stored Procedures in Event Selection tab of the Trace properties.
This way you can make you don't miss any statements in the Text Data.

How to determine which stored procedures are being executed (MSSQL 2008 R2)

I'm having a real problem with my application and SQL Server (2008 R2).
I have a bug whereby a stored procedure is failing because of a misconfigured SQLCMD variable but the call to the stored procedure is in an assembly for which I don't have the source code.
Is there a way to watch which stored procedures are being executed? Or is there a way to determine with an SQL query which stored procedures have been executed and when?
I am really stuck. Please help!
M
You could try running this against your database:
select OBJECT_NAME([object_id], database_id), last_execution_time, execution_count
from sys.dm_exec_procedure_stats
order by last_execution_time desc
Documentation: http://msdn.microsoft.com/en-us/library/cc280701.aspx
That gives you a snapshot at the time of execution what was last run and how many times it's been executed since it was last compiled. The table doesn't unfortunately give a log per-se of the stored procedures getting run, just when they were run last and some other helpful information.
Fore a much more involved approach, you could look at SQL Server Audit, a new feature to SQL Server 2008. I don't have much experience with it, but this should give you a starting point if you're super stuck.

SQL Server trace how to see the TSQL for prepared SQL

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.

SQL Server Profiler - View Dynamic Sql

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.