2016-09-28 3 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

関連する問題