views:

675

answers:

5

I am able to view the Estimated Execution Plan (Management Studio 9.0) for a query without a problem but when it comes to stored procedures I do not see an easy way to do this without copying the code from the ALTER screen and pasting it into a query window, otherwise it will show the plan for the ALTER and not the procedure. Even after doing this, any inputs are missing and I would need to DECLARE them as such.

Is there an easier way to do this on stored procedures?

Edit: I just thought of something that might work but I am not sure.

Could I do the estimated execution plan on

exec myStoredProc 234
A: 

Running the stored procedure in management studio (or query analyser) with show actual execution plan (from the query menu) enabled will show you the plan for the stored procedure after you have run it. If you cant run it there is show estimated execution plan (though in my experience that is often less accurate.)

u07ch
You missed the point of my question. When I use "show estimated exec plan" it shows the plan for the ALTER, not the actual procedure.
Joe Philllips
Sorry i wasnt clear i meant run i meant run the stored procedure rather than run the alter. In a new window exec MySP 'param1', 'param2' and set the estimated execution plan option
u07ch
Ok, but either way I can't run the procedure because it will cause changes to my data.
Joe Philllips
You don't have a test system?
gbn
+1  A: 

When executing a stored procedure in SQL Management Studio 2008 you can click Query -> Include Actual Execution Plan from the menu...its also on the tool bar

After reading through the comments executing seems to be an issue and to solve this issue i would recommend wrapping the execution of the stored procedure in a transaction rolling it back at the end

Jon
I can't actually run it though. It needs to be estimated.
Joe Philllips
You could wrap it in a transaction and just not commit
Jon
Interesting idea
Joe Philllips
A: 

You can also use Profiler to see the execution plan. You'll want to include the Performance : Show Plan Statistics Profile option and be sure to inlcude Binary Data in your columns.

You can then run any query or procedure and see the execution plan.

Edit

If you can't use profiler, and you don't want to open another window I suggest that you include a comment block at the begining of your stored procs. For example imagine the following:

/* 
     Description: This procedure does XYZ etc...
     DevelopedBy: Josh
     Created On:  4/27/09

     Execution: exec my_procName N'sampleparam', N'sampleparam'
*/

ALTER PROCEDURE  my_procName
   @p1 nvarchar(20),
   @p2 nvarchar(20)

AS

What this allows is that you can highlight just the execution purpose and turn on show execution plan. And run it.

JoshBerke
Unfortunately I do not have SQL admin so I can't use the profiler.
Joe Philllips
I actually think you can run profiler as a non-admin if your given the right permissions. Not sure what those are however.
JoshBerke
A: 

Use

SET SHOWPLAN_ALL ON
Go
exec myStoredProc 234
GO
SET SHOWPLAN_ALL OFF
GO

See http://msdn.microsoft.com/en-us/library/aa259203.aspx As long as you aren't using tmp tables i think this will work

UndertheFold
+2  A: 
SET SHOWPLAN_ALL ON
GO

-- FMTONLY will not exec stored proc
SET FMTONLY ON
GO

exec yourproc
GO

SET FMTONLY OFF
GO

SET SHOWPLAN_ALL OFF
GO
Matt Rogish
Why isn't this documented anywhere?
Joe Philllips
'FMT_ONLY' is not a recognized SET option.
Joe Philllips
I think it's FMTONLY not FMT_ONLY
Joe Philllips
Yeah it's FMTONLY my bad
Matt Rogish
Although it's not the visual estimated execution plan, it still does the job!
Joe Philllips