I am running a trace against a SQL Server database that I have that makes extensive use of stored procedures. I have noticed that in some cases, when executing a stored procedure, the RPC:Starting and SP:Starting events have no data in the TextData or BinaryData fields, but when executing a different stored procedure, the same events show the SQL statement that is being executed. Of course, the issue I am trying to investigate is related to the stored procedure whose TextData is not being displayed.
What would prevent SQL Server Profiler (and XEvent Profiler, I checked there as well) from populating the TextData/BinaryData fields of the RPC:Starting and SP:Starting events? I can see the stored procedure is being called, as its name appears in the ObjectName field, and I can see the individual statements within the stored procedure being executed via the SP:StmtStarting event (which does populate the TextData field), but I really want to see what parameters are being passed to it.
Related
I encountered a strange problem today.... I did an sp_helptext on a stored procedure but the stored procedure name in the CREATE PROCEDURE statement (in the result) did not correspond to the name I used in the sp_helptext statement (see screenshot below). I tried selecting from the sys.all_sql_modules and also OBJECT_DEFINITION but it gave the same result.
In my example below I used InvIRCode_GetByInvIRID but the result gave me InvIRCode_GetListByInvIRID
I did a bit of research and it seems it is a known SQL Server bug and happens when a stored procedure was renamed.
My question is this: How can I get the correct source code for a stored procedure?
The interesting thing is that when I right-click on the sp in the Object Explorer of SQL Server Management Studio and select to script the procedure to a new window, it does give the correct code.
Note this warning message is returned by sp_rename:
Caution: Changing any part of an object name could break scripts and stored procedures.
The text in the catalog views is not changed by sp_rename. The proc name will be corrected when you script the proc via SSMS or SMO. You can alter (or drop/create) modules using that script to fix the text in the catalog views after a rename.
I am working with an SSRS Report that uses a stored procedure.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with ALTER PROCEDURE ...
While I can understand the SQL in a stored procedure, I have never used one in an SSRS Report [I only use 'straight' SQL statements].
When I use SQL as my Dataset, I can copy that SQL into SSMS and run it and see the data it returns.
With this stored procedure, how do I execute it in SSMS to see the data it returns? The stored procedure has a sample 'EXEC ...' statement with all the parameters populated ... but when I run that - no data is returned.
The SSRS report runs fine, but I want to be able to work with the stored procedure in SSMS and see the data it is returning. My goal is to be able to run the stored procedure in SSMS and then tweak it.
How do I work with this stored procedure in SSMS so I can look at the output?
If you just want to execute the procedure in SSMS, locate it in the object browser ([DatabaseName]/Programmability/Stored Procedures). RIght-click the procedure and select 'Execute Stored Procedure'
Fill in the parameters and click OK and a script will be generated to run the procedure.
It's a bit overkill but at least everything is there and you can run it whenever you like.
If you want to edit the proc, right-click and choose modify, a new script will be created (the ALTER PROCEDURE script you mentioned). Make changes as required, run the script and that will modify the procedure, then execute the procedure to see the results.
Of course it would be safer to make a copy and edit that, you can also just run the body of the stored proc by commenting out the ALTER PROCEDURE statement until you are happy with it but you may have to declare and variables that are normally passed in as parameters.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with
ALTER PROCEDURE ...
That's the Alter Procedure script. Use this to edit a stored procedure.
In other words, edit the SQL code you want to optimize, then run the whole script to save the changes.
How do I work with this stored procedure in SSMS so I can look at the
output?
In SSMS use the syntax for stored procedures:
EXEC myspname paramter1, parameter2, param...
Where parameter1, parameter2, etc. are the parameters described in the ALTER Procedure script, directly after the ALTER PROCEDURE myspname. Parameters are preceded by the # symbol.
As you type-in the EXEC procedure command pop-up hints should appear describing the parameter.
Without knowing the code to the stored procedure, it could be doing any number of things based on what is passed to it by parameter. A stored procedure can do DDL and DML queries, and does not necessarily have to select anything at all for output.
Is there a way to have a SQL Server stored procedure trigger a query without editing the stored procedure code?
I am looking for a way to do something along the lines of a CREATE TRIGGER, but have it be triggered by a stored procedure instead of table update without altering the original code for the stored procedure.
I would create a trigger for when the table updates, but the stored procedure that updates the table updates it tens of thousands of times, so that is unfeasible.
select * from sys.dm_exec_procedure_stats
I would check the "execution_count" to the system table listed above. You can create a stored procedure and then create a job that runs that stored procedure at a specified interval.
I've got a stored procedure with an int output parameter. If I run SQL Server Profiler, execute the stored procedure via some .Net code, and capture the RPC:Completed event, the Text Data looks like this:
declare #p1 int
set #p1=13
exec spStoredProcedure #OutParam=#p1 output
select #p1
Why does it look like it's getting the value of the output parameter before executing the stored procedure?
I found an answer that it is RPC:completed event class. so it already know the result at that time. But I cant understand why exec statement is there, after completion of RPC:completed event.
The RPC Completed TextData you see in the Profiler (or Extended Events) trace is just a rendering of the RPC request, not the actual statement that was executed by SQL Server. This facilitates copy/paste of the text into an SSMS window for ad-hoc execution as a SQL batch.
Since the actual output value is known after the RPC has completed, the trace text uses the actual value to initialize the parameter variable. It would probably be clearer it was initialized to NULL prior to execution.
Does SqlServer has to start a Scan to execute a stored procedure?
In Sql Profiler I can see this:
RPC Starting ( exec sp_Edu3_SelectExamSession #ExamSessionId=N'AccessCode39361814' )
Scan:Started
Scan:Started
Scan:Started
RPC Completed ( exec sp_Edu3_SelectExamSession #ExamSessionId=N'AccessCode39361814' )
Can I somehow see what's happening in the Stored Procedure? Different queries are done in that SP, but they do not seem to appear in Sql Profiler (maybe I need to check some more events?)
The Scan:Started are probably scans by the queries in the sp? Or not?
When you launch profiler, there is a template where you can see stored proc line by line execution. It's something like "SQLProfilerTSQL_SPs" from memory.
The Scan event may be associated, it may not: it depends on the filters you have set.