<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlserver.ro/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Script Repository</title><link>http://sqlserver.ro/forums/38/ShowForum.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Get query text and query plans for cached query plans</title><link>http://sqlserver.ro/forums/thread/412.aspx</link><pubDate>Tue, 17 Oct 2006 08:15:13 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:412</guid><dc:creator>xmldeveloper</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/412.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=38&amp;PostID=412</wfw:commentRss><description>&lt;p&gt;&lt;strong&gt;Source: &lt;a href="http://blogs.msdn.com/cbiyikoglu/archive/2005/11/03/488920.aspx" id="bp___ctl00___RecentPosts___postlist___EntryItems_ctl02_PostTitle"&gt;Forced and Simple Parameterization in SQL Server 2005&lt;/a&gt;&amp;nbsp;(&lt;a class="headermaintitle" href="http://blogs.msdn.com/cbiyikoglu/default.aspx" id="bp___ctl00___bth___BlogTitle"&gt;&lt;font color="#000000"&gt;TSQLNet Weblog - Cihangir Biyikoglu&lt;/font&gt;&lt;/a&gt;)&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;SELECT &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;c.[text],&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;a.refcounts,&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;a.usecounts,&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;d.query_plan &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;FROM sys.dm_exec_cached_plans a &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;OUTER APPLY sys.dm_exec_plan_attributes(a.plan_handle) b &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;OUTER APPLY sys.dm_exec_sql_text(CAST(b.VALUE AS VARBINARY)) c &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;OUTER APPLY sys.dm_exec_query_plan(a.plan_handle) d &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;WHERE b.attribute=&amp;#39;sql_handle&amp;#39; AND a.cacheobjtype = &amp;#39;Compiled Plan&amp;#39; &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;strong&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;ORDER BY a.usecounts DESC&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;font face="Courier New"&gt;&lt;/font&gt;</description></item></channel></rss>