Query text during caching in SQL 2005

  • Comments posted to this topic are about the item Query text during caching in SQL 2005

    Thanks

  • Nice question ... learned something

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Very bad worded question:

    Query text during caching in SQL 2005

    This is related to SQL Server caching. What should be the output of following T-SQL query:

    USE AdventureWorks2008R2

    GO

    EXEC uspPrintError

    GO

    SELECT st.text QueryText

    FROM sys.dm_exec_cached_plans

    CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st

    WHERE text LIKE N'%uspPrintError%';

    In the title, it refers to sql2005, than it uses AdventureWorks2008R2

    In sql2005, I get an error:

    "plan_handle" is not a recognized table hints option.

    I also tried to run

    SELECT text FROM sys.dm_exec_sql_text(0x0500FF7F79BB1808C8808A04000000000000000000000000)

    But it returns this:

    -- uspPrintError prints error information about the error that caused

    -- execution to jump to the CATCH block of a TRY...CATCH construct.

    -- Should be executed from within the scope of a CATCH block otherwise

    -- it will return without printing any error information.

    CREATE PROCEDURE [dbo].[uspPrintError]

    AS

    BEGIN

    SET NOCOUNT ON;

    -- Print error information.

    PRINT 'Error ' + CONVERT(varchar(50), ERROR_NUMBER()) +

    ', Severity ' + CONVERT(varchar(5), ERROR_SEVERITY()) +

    ', State ' + CONVERT(varchar(5), ERROR_STATE()) +

    ', Procedure ' + ISNULL(ERROR_PROCEDURE(), '-') +

    ', Line ' + CONVERT(varchar(5), ERROR_LINE());

    PRINT ERROR_MESSAGE();

    END;

    At end I answered BOTH: EXEC ... and text for the stored proc.

    The explanation seems to be approximate!

  • Good question. I answered wrong; I thought all statements were cached.

    Hardik, do you have any references where I can read more about this?


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Carlo Romagnano (11/14/2010)


    Very bad worded question:

    Query text during caching in SQL 2005

    This is related to SQL Server caching. What should be the output of following T-SQL query:

    USE AdventureWorks2008R2

    GO

    EXEC uspPrintError

    GO

    SELECT st.text QueryText

    FROM sys.dm_exec_cached_plans

    CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st

    WHERE text LIKE N'%uspPrintError%';

    In the title, it refers to sql2005, than it uses AdventureWorks2008R2

    In sql2005, I get an error:

    "plan_handle" is not a recognized table hints option.

    I also tried to run

    SELECT text FROM sys.dm_exec_sql_text(0x0500FF7F79BB1808C8808A04000000000000000000000000)

    But it returns this:

    -- uspPrintError prints error information about the error that caused

    -- execution to jump to the CATCH block of a TRY...CATCH construct.

    -- Should be executed from within the scope of a CATCH block otherwise

    -- it will return without printing any error information.

    CREATE PROCEDURE [dbo].[uspPrintError]

    AS

    BEGIN

    SET NOCOUNT ON;

    -- Print error information.

    PRINT 'Error ' + CONVERT(varchar(50), ERROR_NUMBER()) +

    ', Severity ' + CONVERT(varchar(5), ERROR_SEVERITY()) +

    ', State ' + CONVERT(varchar(5), ERROR_STATE()) +

    ', Procedure ' + ISNULL(ERROR_PROCEDURE(), '-') +

    ', Line ' + CONVERT(varchar(5), ERROR_LINE());

    PRINT ERROR_MESSAGE();

    END;

    At end I answered BOTH: EXEC ... and text for the stored proc.

    The explanation seems to be approximate!

    Thanks Carlo for pointing out the typo mistake on header (title). This behavior is same for SQL Server 2005 & in SQL 2008 and I used AdventureWorks2008R2 database so has idea that it is SQL2k8 R2 database provided by Microsoft.

    Also, thanks for the validation of explanation provided.

    Thanks

  • Nice question, definately learned something today.

    However, if I run the query in SQL Server 2008 using the AdventureWorks2008 database, I get an extra result: the select query with the cross apply used in the question itself.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Good question, Hardik!

    I answered wrong because I was distracted by the use of the stored procedure uspPrintError 🙂

    Thanks & Regards,
    Nakul Vachhrajani.
    http://nakulvachhrajani.com

    Follow me on
    Twitter: @sqltwins

  • Execute the batch without the 'GO'

    EXEC uspLogError

    SELECT st.text QueryText,objtype

    FROM sys.dm_exec_cached_plans

    CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st

    WHERE text LIKE N'%uspLogError%'

    Result:

    CREATE PROCEDURE [dbo].[uspLogError] ....,'Proc'

    EXEC uspLogError SELECT st.text QueryText FROM sys.dm_exec_cached_plans ....,'Adhoc'

    The body of the stored procedure is cached in the objtype = 'Proc' (see also other info about the proc). the second record is the plan stored with objtype = 'Adhoc'

    See also: http://msdn.microsoft.com/en-us/library/ms187404.aspx

  • Any BOL links you can provide regarding this? There wasn't one in the explanation.

  • Good question, thanks for taking the time to help educate the SQL community.

    I look at these DMV's a lot so thankfully answered this one correctly.

    Hope this helps,
    Rich

    [p]
    [/p]

  • Great question! Learned something new. Thanks.

  • It is correct to say that removing the GO between the EXEC call and the SELECT against the DMV changes the behavior. Clearly there is some caching going on at least for the duration of the batch itself, and removing it from cache when the batch completes.

  • Learned something new from the discussion. Thanks guys.

  • The distribution of answers is almost even. That shows that this was a real brain tickler.

  • Carlo Romagnano (11/14/2010)


    In sql2005, I get an error:

    "plan_handle" is not a recognized table hints option.

    Carlo,

    Please check compat level of your SQL Server 2005 AdventureWorks database. If the level is 80 then you should get "plan_handle" is not a recognized table hints option error, but if it is 90 as it should be then the script should run just fine.

    Oleg

Viewing 15 posts - 1 through 15 (of 25 total)

You must be logged in to reply to this topic. Login to reply