November 14, 2010 at 8:09 am
Comments posted to this topic are about the item Query text during caching in SQL 2005
Thanks
November 14, 2010 at 8:16 am
November 14, 2010 at 3:57 pm
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!
November 14, 2010 at 5:22 pm
Good question. I answered wrong; I thought all statements were cached.
Hardik, do you have any references where I can read more about this?
November 14, 2010 at 9:38 pm
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
November 15, 2010 at 1:37 am
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
November 15, 2010 at 1:39 am
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
November 15, 2010 at 2:02 am
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
November 15, 2010 at 5:40 am
Any BOL links you can provide regarding this? There wasn't one in the explanation.
November 15, 2010 at 6:23 am
November 15, 2010 at 6:31 am
Great question! Learned something new. Thanks.
November 15, 2010 at 7:10 am
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.
November 15, 2010 at 7:37 am
Learned something new from the discussion. Thanks guys.
November 15, 2010 at 8:05 am
The distribution of answers is almost even. That shows that this was a real brain tickler.
November 15, 2010 at 9:39 am
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