2015-10-08 44 views

回答

0

不幸的是,我不認爲直接是可能的。相反,你需要查詢和分析實際的視圖定義:

SELECT VIEW_DEFINITION 
FROM INFORMATION_SCHEMA.VIEWS 
WHERE 
TABLE_NAME = ?; 
+1

View_definition返回視圖的孔洞腳本。我需要腳本中提到的表格列表。 –

+0

不幸的是,我不相信這是可能的。相反,您需要查詢和分析實際的視圖定義。讓我看看我還能找到你 –

0

沒有這是不可能的。您必須查看視圖的定義並手動完成。

0

mysql> CREATE VIEW vw_test AS 
    -> SELECT * FROM table1 JOIN table2 ON table1.id = table2.id; 
Query OK, 0 rows affected (0.00 sec) 

mysql> SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS 
    -> WHERE TABLE_NAME = 'vw_test'; 
+------------------------------------------------------------------+ 
| VIEW_DEFINITION             | 
+------------------------------------------------------------------+ 
| select * from table1 join table2 on table1.id = table2.id;  | 
+------------------------------------------------------------------+ 
1 row in set (0.00 sec) 

那麼你可以使用以下任一工具來分析表名:

  1. 特倫斯·帕爾的ANTLR解析器生成器(Java的,但可以生成許多目標語言中的任何一個解析器)有幾種可用的SQL語法,包括一對PL/SQL,一個用於SQL Server SELECT語句,一個用於mySQL,另一個用於ISO SQL - (http://www.antlr.org/grammar/list)。

    我把這個從蘇答案在這裏:SQL parser library for Java - Retrieve the list of table names present in a SQL statement

  2. Data Tools Project - SQL Development Toolshttp://www.eclipse.org/datatools/project_sqldevtools/)。

    以下是SQL Query Parserhttp://www.eclipse.org/datatools/project_sqldevtools/sqltools_doc/SQL%20Query%20Parser%20User%20documentation.htm)的文檔。

  3. 下面是與如何 「Get columns and tables in SQL script (Java version)http://www.dpriver.com/blog/list-of-demos-illustrate-how-to-use-general-sql-parser/get-columns-and-tables-in-sql-script/
  4. 或者自己編寫基於以下(這裏找到 - http://www.sqlparser.com/fetch-table-column-name-example-extact-all-table-field-name.php)定製的MySQL PROC描述一個博客:

    SELECT c_mandant, hist_datum, parkey1, parkey2, funktionscode, ma_parkey, me_parkey 
    , CASE WHEN EXISTS (SELECT 1 
             FROM CDS_H_GRUPPE GRP1 
             WHERE GRP1.c_mandant = c_mandant 
             AND GRP1.hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1) 
             AND GRP1.funktionscode = 'H' 
             AND GRP1.parkey1  = ma_parkey) 
          THEN 1 
         ELSE NULL 
        END MA_ME 
    , CASE WHEN EXISTS (SELECT 1 
             FROM CDS_H_GRUPPE GRP2 
             WHERE GRP2.c_mandant  = c_mandant 
             AND GRP2.hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1) 
             AND GRP2.funktionscode = 'U' 
             AND GRP2.parkey1  = me_parkey) 
          THEN 1 
         ELSE NULL 
        END ME_MA 
    , ROW_NUMBER() OVER (PARTITION BY c_mandant, ma_parkey, me_parkey ORDER BY c_mandant, ma_parkey, me_parkey) ANZ_MA 
        FROM (SELECT c_mandant, hist_datum, parkey1, parkey2, funktionscode 
         , CASE WHEN funktionscode = 'U' 
            THEN parkey1 
           ELSE parkey2 
          END MA_PARKEY 
         , CASE WHEN funktionscode = 'U' 
            THEN NULL 
           ELSE parkey1 
          END ME_PARKEY 
         FROM 
          CDS_H_GRUPPE 
        WHERE 
          funktionscode IN ('U', 'H') 
         AND hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1) 
        ) 
    
相關問題