Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:28:00+00:00 2026-06-17T09:28:00+00:00

When I execute this simple SP and then check for the query plan from

  • 0

When I execute this simple SP and then check for the query plan from the DMV query that follows, the query plan comes back as NULL. But if I comment out the else block in the SP (or change it to not reference temp table), the DMV query does show a query plan. Any idea what is causing this difference? And does a NULL query plan mean that nothing was cached? I’m using SQL Server 2008 R2.

CREATE PROCEDURE dbo.SampleSp
(
    @Option TINYINT
)
AS
    DECLARE @RowCount INT;

    -- Get ID into local temp table

    CREATE TABLE #P (ID INT PRIMARY KEY);
    INSERT INTO #P
    VALUES (1), (2)

    IF @Option = 1
    BEGIN
        SELECT ID FROM #P
    END
    ELSE
    BEGIN
        SELECT ID FROM #P
    END
GO

    --Call SP
    EXEC dbo.SampleSp 1
GO
    --DMV Query to find query_plan
    SELECT  
            CP.objtype, 
            CP.usecounts AS Ct, 
            ST.last_execution_time,
            SUBSTRING(Text, (statement_start_offset / 2) + 1, (CASE statement_end_offset WHEN -1 THEN DATALENGTH(Text) ELSE statement_end_offset END  - ((statement_start_offset / 2) + 1))) AS query,
            T.text,
            PL.query_plan
    FROM sys.dm_exec_cached_plans AS CP WITH(NOLOCK)
    CROSS APPLY sys.dm_exec_sql_text(CP.plan_handle) AS T
    OUTER APPLY sys.dm_exec_query_plan(CP.plan_handle) AS PL
    LEFT OUTER JOIN sys.dm_exec_query_stats AS ST WITH(NOLOCK) ON ST.plan_handle = CP.plan_handle
    WHERE 1=1
    AND ST.last_execution_time > DATEADD(MINUTE, -5, GETDATE())
    AND T.text LIKE '%SampleSp%'
    AND T.text NOT LIKE '%dm_exec_cached_plans%'
    ORDER BY ST.last_execution_time DESC
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:28:01+00:00Added an answer on June 17, 2026 at 9:28 am

    I believe I figured it out. It seems that the plan will show up as NULL until every branch in the SP has been executed. (You can see this by making another call to the SP with a parameter value of 2, and then the plan does show up.) If only part of the SP has been executed, SQL Server does cache than plan internally (you can see this by looking at usecounts, plus some other more extensive testing I did to prove this), but it will not show up in the DMV query I have used. Which raises the question: How do you view the plan of just the part of the SP that has been cached?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I execute this query: Dim Var Var = ("select max(Autonumber) from tblAutonumber") DoCmd.RunSQL
I have this sql query in python using psycopg: cr.execute('select id from test_table where
Why does this quite simple xquery takes 10min to execute in sql server (the
I tried to execute this query and i can´t understand what´s wrong with it.
When I execute this code the user is correct but the pass is not,
I want to execute this script (view source) that uses Google Translate AJAX API
I have this simple code to test that a DB is ready: Function testlocalcon()
I created a class derived from TThread that executes in background a query. I
Note: Now that I know where the issue comes from, I modified the question.
I'm trying to execute this code (this is a minimal sample in order to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.