Here I am trying to read sql server plan (Execution query Plan) file from ssms query.
DECLARE @plancontents xml,@xml XML SET @plancontents=(SELECT * FROM OPENROWSET(BULK 'P:\Users\hpatterson\Documents\myplan.sqlplan', SINGLE_CLOB) x) --Get rid of the namespace stuff --SET @plancontents=REPLACE(@plancontents,'xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan"','') --Now put that into our XML variable SET @xml=cast(@plancontents as xml) --DECLARE @xml XML --select @xml=query_plan from sys.dm_exec_query_plan select @xml ---- notice the removed ---- xmlns=http://schemas.microsoft.com/sqlserver/2004/07/showplan ---- attribute -- go through all the execution plan nodes, get the attributes and sort on them ;with xmlnamespaces (default 'http://schemas.microsoft.com/sqlserver/2004/07/showplan') SELECT c.value('.[1]/@PhysicalOp', 'nvarchar(max)') as PhysicalOp, c.value('.[1]/@LogicalOp', 'nvarchar(max)') as LogicalOp, c.value('.[1]/@AvgRowSize', 'nvarchar(max)') as AvgRowSize, c.value('.[1]/@TableCardinality', 'nvarchar(max)') as TableCardinality, c.value('.[1]/@Parallel', 'nvarchar(max)') as Parallel, c.value('.[1]/@EstimateRebinds', 'nvarchar(max)') as EstimateRebinds, c.value('.[1]/@EstimateRewinds', 'nvarchar(max)') as EstimateRewinds, c.value('.[1]/@NodeId', 'nvarchar(max)') as NodeId, c.value('.[1]/@EstimatedTotalSubtreeCost', 'nvarchar(max)') as EstimatedTotalSubtreeCost, c.value('.[1]/@EstimateRows', 'nvarchar(max)') as EstimateRows, c.value('.[1]/@EstimateIO', 'nvarchar(max)') as EstimateIO, c.value('.[1]/@EstimateCPU', 'nvarchar(max)') as EstimateCPU, -- this returns just the node xml for easier inspection c.query('.') as ExecPlanNode FROM -- this returns only nodes with the name RelOp even if they are children of children @xml.nodes('//child::RelOp') T(c) ORDER BY EstimatedTotalSubtreeCost DESC