2016-09-28 2 views
0

XML 열에 다음 구조가 있습니다. 그것은 show stored procedures이고 목록은 used tables and columns입니다.XQuery 크로스 적용시 추가 레코드가 반환됩니다.

추가 기록이 있습니다. 구조 및 쿼리 아래 참조하십시오

CREATE TABLE #T (
    ID int primary key, 
    MetaData xml 
) 

insert into #T values(1, 
'<root> 
    <object name="myProc"> 
     <tables> 
      <table database="LynxReporting" schema="Secure" name="factPortCore"> 
       <columns> 
        <column name="SnapshotKey"/> 
        <column name="SnapshotDt"/> 
       </columns> 
      </table> 
      <table database="LynxSnapshot" schema="dbo" name="Loan"> 
       <columns> 
        <column name="LoanProgId"/> 
        <column name="FinType"/> 
       </columns> 
      </table> 
      <table database="LynxReporting" schema="dbo" name="dimGLSrcSysId"> 
       <columns> 
        <column name="GLSrcSysId"/> 
       </columns> 
      </table> 
     </tables> 
    </object> 
    <object name="usp_Recon"> 
     <tables> 
      <table database="LynxReporting" schema="Secure" name="dimAppSysId"> 
       <columns> 
        <column name="AppSysId"/> 
        <column name="AppSysIdLongDesc"/> 
       </columns> 
      </table> 
     </tables> 
    </object> 
</root>') 

SELECT 
    t.x.value('@name', 'varchar(max)') as TableName 
    , c.x.value('@name', 'varchar(max)') as ColumnName 
FROM #T 
    CROSS APPLY MetaData.nodes('root/object/tables/table') as t(x) 
    CROSS APPLY MetaData.nodes('root/object/tables/table/columns/column') as c(x) 
order by 1,2 

내가 무엇을 얻을 (당신이 관련이없는 행 참조)

╔═══════════════╦══════════════════╗ 
║ TableName  ║ ColumnName  ║ 
╠═══════════════╬══════════════════╣ 
║ Loan   ║ AppSysId   ║ 
╠═══════════════╬══════════════════╣ 
║ Loan   ║ AppSysIdLongDesc ║ 
╠═══════════════╬══════════════════╣ 
║ Loan   ║ FinType   ║ 
╠═══════════════╬══════════════════╣ 
║ Loan   ║ GLSrcSysId  ║ 
╠═══════════════╬══════════════════╣ 
║ Loan   ║ LoanProgId  ║ 
╠═══════════════╬══════════════════╣ 
║ Loan   ║ SnapshotDt  ║ 
╠═══════════════╬══════════════════╣ 
║ Loan   ║ SnapshotKey  ║ 
╠═══════════════╬══════════════════╣ 
║ dimAppSysId ║ AppSysId   ║ 
╠═══════════════╬══════════════════╣ 
║ dimAppSysId ║ AppSysIdLongDesc ║ 
╠═══════════════╬══════════════════╣ 
║ dimAppSysId ║ FinType   ║ 
╠═══════════════╬══════════════════╣ 
║ dimAppSysId ║ GLSrcSysId  ║ 
╠═══════════════╬══════════════════╣ 
║ dimAppSysId ║ LoanProgId  ║ 
╠═══════════════╬══════════════════╣ 
║ dimAppSysId ║ SnapshotDt  ║ 
╠═══════════════╬══════════════════╣ 
║ dimAppSysId ║ SnapshotKey  ║ 
╠═══════════════╬══════════════════╣ 
║ dimGLSrcSysId ║ AppSysId   ║ 
╠═══════════════╬══════════════════╣ 
║ dimGLSrcSysId ║ AppSysIdLongDesc ║ 
╠═══════════════╬══════════════════╣ 
║ dimGLSrcSysId ║ FinType   ║ 
╠═══════════════╬══════════════════╣ 
║ dimGLSrcSysId ║ GLSrcSysId  ║ 
╠═══════════════╬══════════════════╣ 
║ dimGLSrcSysId ║ LoanProgId  ║ 
╠═══════════════╬══════════════════╣ 
║ dimGLSrcSysId ║ SnapshotDt  ║ 
╠═══════════════╬══════════════════╣ 
║ dimGLSrcSysId ║ SnapshotKey  ║ 
╠═══════════════╬══════════════════╣ 
║ factPortCore ║ AppSysId   ║ 
╠═══════════════╬══════════════════╣ 
║ factPortCore ║ AppSysIdLongDesc ║ 
╠═══════════════╬══════════════════╣ 
║ factPortCore ║ FinType   ║ 
╠═══════════════╬══════════════════╣ 
║ factPortCore ║ GLSrcSysId  ║ 
╠═══════════════╬══════════════════╣ 
║ factPortCore ║ LoanProgId  ║ 
╠═══════════════╬══════════════════╣ 
║ factPortCore ║ SnapshotDt  ║ 
╠═══════════════╬══════════════════╣ 
║ factPortCore ║ SnapshotKey  ║ 
╚═══════════════╩══════════════════╝ 

내가 원하는 무엇 : 보시다시피

╔════════════╦═══════════════╦══════════════════╗ 
║ ObjectName ║ TableName  ║ ColumnName  ║ 
╠════════════╬═══════════════╬══════════════════╣ 
║ myProc  ║ Loan   ║ FinType   ║ 
╠════════════╬═══════════════╬══════════════════╣ 
║ myProc  ║ Loan   ║ LoanProgId  ║ 
╠════════════╬═══════════════╬══════════════════╣ 
║ usp_Recon ║ dimAppSysId ║ AppSysId   ║ 
╠════════════╬═══════════════╬══════════════════╣ 
║ usp_Recon ║ dimAppSysId ║ AppSysIdLongDesc ║ 
╠════════════╬═══════════════╬══════════════════╣ 
║ myProc  ║ dimGLSrcSysId ║ GLSrcSysId  ║ 
╠════════════╬═══════════════╬══════════════════╣ 
║ myProc  ║ factPortCore ║ SnapshotDt  ║ 
╠════════════╬═══════════════╬══════════════════╣ 
║ myProc  ║ factPortCore ║ SnapshotKey  ║ 
╚════════════╩═══════════════╩══════════════════╝ 

, 나는 또한 원하는 ObjectName 열이 있어야합니다. 검색어를 어떻게 변경해야할지 모르겠습니다. 어떤 도움을 주셔서 감사합니다.

답변

0

십자가는

SELECT 
    r.o.value('@name', 'varchar(max)') as ObjectName 
    , t.x.value('@name', 'varchar(max)') as TableName 
    , c.x.value('@name', 'varchar(max)') as ColumnName 
FROM #T 
    CROSS APPLY MetaData.nodes('root/object') as r(o) 
    CROSS APPLY r.o.nodes('tables/table') as t(x) 
    CROSS APPLY t.x.nodes('columns/column') as c(x) 
+0

대! 당신을 감사 이전 세트에서 값을 적용합니다. – FLICKER

관련 문제