Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

SP_helptext Issues

$
0
0

I am trying to list the execution plan in xml (SET SHOWPLAN ON) of a given sp in a query that returns other things as well.  

To do that I have been trying to use sp_helptext to return the contents of the given sp in a query but I then want the execution plan of that query itself.  

Basically I am trying to view all SPs that hit a given index and then list the execution plan for the sp that caused the most load on the server and I am trying to do all of this in one query.  

I have had some luck pulling the indexes of all the tables out and wrapping that up into an sp itself but I am having trouble pulling out the sp that hits an index the most and showing the execution plan for that sp.  Can anyone help me out?  

Below is what I have so far without the sp_helptext and execution plan part:  

sp_ListIndexAll @DB = 'AdventureWorks2012'

IF OBJECT_ID ('sp_ListIndexAll') IS NOT NULL
DROP PROCEDURE sp_ListIndexAll;
GO
CREATE PROCEDURE sp_ListIndexAll

/*----------------------------------------------------------------------------------------
-- OBJECT NAME: sp_ListIndexAll
-- LOCATION: (AdventureWorks2012) - Test Database
-- AUTHOR: 
-- DATE: 8/27/2013
INPUT PARAMETERS: @DB
-----------------------------------------------------------------------------------------*/


@DB nvarchar(100) = N'%'
AS 
SET NOCOUNT ON;

DECLARE @ListAllIndex VARCHAR(8000)
SET @ListAllIndex ='USE ['+@DB+']; DECLARE @Yes varchar(3) SET @Yes = ''Yes''
DECLARE @No varchar(2) SET @No = ''No'' 
DECLARE @C varchar(1) SET @C = ''C'' 
DECLARE @NC varchar(2) SET @NC = ''NC''  
SELECT TableName = object_name(i.object_id), 
         [Unique] = CASE 
                      WHEN s.index_id = 1 THEN @Yes 
                      ELSE @No 
                    END, 
         [Clustered] = CASE 
                         WHEN s.index_id = 1 THEN @C 
                         ELSE @NC
                       END, 
IndexName = object_name(s.object_id),
Date_Modified_By_User = s.last_user_update,
Date_Modified_By_System = s.last_system_update,
s.user_seeks,
s.last_user_seek,
s.user_lookups,
s.last_user_scan,
s.user_updates,
s.last_user_update,
g.page_count,
g.avg_page_space_used_in_percent,
g.fragment_count
FROM     sys.indexes i 
INNER JOIN sys.dm_db_index_usage_stats s
  ON i.object_id = s.object_id
INNER JOIN sys.dm_db_index_physical_stats (NULL, NULL, NULL, NULL, ''sampled'') g
  ON i.index_id = g.index_id
  AND i.object_id = g.object_id
INNER JOIN sys.tables t 
           ON i.object_id = t.object_id 
WHERE (0=0)
'

EXEC (@ListAllIndex)
GO


Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>