Retrieves the query plan for a specified server process (the target process) and a SQL statement. This function is called several times by sp_showplan because a built-in function can return just one value per call, but sp_showplan must return several values to the client.
show_plan(spid, batch_id, context_id, statement_number)
Validates parameter values that sp_showplan cannot validate. -1 is passed in when the user executes sp_showplan without a value for a parameter. Only the spid value is required.
If just a process ID is received, then show_plan returns the batch ID, the context ID, and the statement number in three successive calls by sp_showplan.
Find the E_STMT pointer for the specified SQL statement number.
Retrieves the target process’s query plan for the statement. For parallel worker processes the equivalent parent plan is retrieved to reduce performance impact.
Synchronizes access to the query plan with the target process.
if (@batch_id is NULL) begin /* Pass -1 for unknown values. */ select @return_value = show_plan(@spid, -1, -1, -1) if (@return_value < 0) return (1) else select @batch_id = @return_value select @return_value = show_plan(@spid, @batch_id, -1, -1) if (@return_value < 0) return (1) else select @context_id = @return_value select @return_value = show_plan(@spid, @batch_id, @context_id, -1) if (@return_value < 0) return (1) else begin select @stmt_num = @return_value return (0) end end
The first returns the batch ID
The second returns the context ID
The third displays the query plan, and returns the current statement number.
For a statement that is not performing well, you can change the plans by altering the optimizer settings or specifying an abstract plan.
When you specify the first int variable in the existing show_plan argument as “-”, show_plan treats the second parameter as a SSQLID.
See also sp_showplan in Reference Manual: Procedures
ANSI SQL – Compliance level: Transact-SQL extension.
The permission checks for show_plan differ based on your granular permissions settings.
Granular Permissions | Description |
---|---|
Enabled | With granular permissions enabled, you must be a user with monitor qp performance permission to execute show_plan. |
Disabled | With granular permissions disabled, you must be a user with sa_role to execute show_plan. |