show_plan

Description

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.

Syntax

show_plan(spid, batch_id, context_id, statement_number)

Parameters

spid

is the process ID for any user connection.

batch_id

is the unique number for a batch.

context_id

is the unique number of every procedure (or trigger).

statement_number

is the number of the current statemenmt within a batch.

Examples

Example 1

In the following example, show_plan performs the following:

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

As the example shows, call show_plan three times for a spid :

Usage

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.

NoteA single entry in the statement cache may be associated with multiple, and possibly different, SQL plans. show_plan displays only one of them.

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

See also

Procedures sp_showplan