LINUX:如何输出SQL脚本中使用的表

时间:2018-11-23 14:16:18

标签: linux shell unix awk grep

人。

我有大量的SQL脚本(超过1000个文件),而且我必须列出一些有关它们的信息。主要问题是列出每个脚本上的所有表和正在使用的操作。我需要主要查找SELECT,INSERT,DELETE和UPDATE操作。

选择示例:

     SELECT column1, column2
       FROM SCHEMA1.TABLE_A A, SCHEMA1.TABLE_B B,
            SCHEMA3.TABLE_K, SCHEMA3.TABLE_M,
            SCHEMA4.TABLE N
      INNER JOIN SCHEMA2.TABLE_C C ON C.COLUMN_XX = A.COLUMN_XX
       LEFT JOIN SCHEMA2.TABLE_D D ON D.COLUMN_YY = A.COLUMN_YY
 FULL OUTER JOIN SCHEMA2.TABLE_E E ON E.COLUMN_ZZ = B.COLUMN_ZZ
      WHERE B.COLUMN_AA = A.COLUMN_AA
        AND K.COLUMN_KK = A.COLUMN_KK
        AND M.COLUMN_MM = A.COLUMN_MM
        AND N.COLUMN_NN = A.COLUMN_NN;

将要进行选择的输出(顺序不重要):

SCHEMA1.TABLE_A | SELECT
SCHEMA1.TABLE_B | SELECT
SCHEMA3.TABLE_K | SELECT
SCHEMA3.TABLE_M | SELECT
SCHEMA4.TABLE_N | SELECT
SCHEMA2.TABLE_C | SELECT
SCHEMA2.TABLE_D | SELECT
SCHEMA2.TABLE_E | SELECT

插入示例:

INSERT INTO SCHEMA1.TABLE_A (COLUMN1,COLUMN2) VALUES ('VALUE_1','VALUE_2');
INSERT INTO SCHEMA1.TABLE_B
(COLUMN_1,COLUMN2)
VALUES
('VALUE_1','VALUE_2');
INSERT INTO SCHEMA2.TABLE_C SELECT * FROM SCHEMA5.TABLE_C;

插入的输出将是(顺序不重要):

SCHEMA1.TABLE_A | INSERT
SCHEMA1.TABLE_B | INSERT
SCHEMA2.TABLE_C | INSERT
SCHEMA5.TABLE_C | SELECT (IF IT IS "OK" TO PUT SOMETHING LIKE 'SELECT (INSERT)' WOULD BE BETTER.. IF NOT, THAT'S OK)

删除示例:

DELETE FROM SCHEMA3.TABLE_K WHERE K.COLUMN_KK = 'A';
DELETE FROM SCHEMA1.TABLE_A A
 INNER JOIN SCHEMA2.TABLE_C C ON C.COLUMN_XX = A.COLUMN_XX
 WHERE A.COLUMN_AA = 'A';

将要删除的输出(订购时不重要):

SCHEMA3.TABLE_K | DELETE
SCHEMA1.TABLE_A | DELETE
SCHEMA2.TABLE_C | JOIN (IF IT IS "OK" TO PUT SOMETHING LIKE 'JOIN (DELETE)' WOULD BE BETTER... IF NOT, THAT'S OK)

更新示例:

UPDATE SCHEMA1.TABLE_A SET COLUMN_AA = 'A';
UPDATE SCHEMA2.TABLE_C SET COLUMN_CC = (SELECT COLUMN_CC FROM SCHEMA5.TABLE_C)
 WHERE EXISTS (SELECT * FROM SCHEMA5.TABLE_C C2 WHERE C2.COLUMN_CC = C.COLUMN_CC);
UPDATE SCHEMA2.TABLE_D D
 INNER JOIN SCHEMA1.TABLE_A A ON D.COLUMN_ID = A.COLUMN_ID
   SET D.COLUMN_DD = A.COLUMN_DD;

将要进行更新的输出(不重要):

SCHEMA1.TABLE_A | UPDATE
SCHEMA2.TABLE_C | UPDATE
SCHEMA5.TABLE_C | SELECT
SCHEMA2.TABLE_D | UPDATE
SCHEMA1.TABLE_A | JOIN (IF IT IS "OK" TO PUT SOMETHING LIKE 'JOIN (UPDATE)' WOULD BE BETTER... IF NOT, THAT'S OK)

关于哪种工具最适合使用它的任何线索,如何克服主要问题的线索,例如FROM之后的换行符,一项特定工作之后如何获取单词(例如FROM子句之后如何获取内容),如何确定在JOIN子句等上的主要操作是最受欢迎的。

谢谢大家。

2 个答案:

答案 0 :(得分:0)

嗯,很难将所有SQL语法(PostgeSQL,Oracle,MS ..)解释为正则表达式。

无论如何,以下是AWK的一些初始示例,该示例应适用于您的SELECT和INSERT语句示例:

awk '
BEGIN { RS="[ ,;\n]"; }
/^SELECT$|^INSERT$/ { operation=$1; watchtable=0; }
/^FROM$|^JOIN$|^INTO$/ { watchtable=1; }
/^WHERE$|^INNER$|^OUTER$|^LEFT$|^RIGHT$|^ON$|^ORDER$|^GROUP$|^\(/ { watchtable=0; }
watchtable && /^[^\.]+\.[^\.]+$/ { print($0 " | " operation); }
' SELECT.sql INSERT.sql

您可以继续进行调整以获取更多,更可靠的代码。

注意:根据您的示例,我使任务更加容易,并假设您的表始终以schema.table的形式出现。

SELECT的输出:

SCHEMA1.TABLE_A | SELECT
SCHEMA1.TABLE_B | SELECT
SCHEMA3.TABLE_K | SELECT
SCHEMA3.TABLE_M | SELECT
SCHEMA4.TABLE | SELECT
SCHEMA2.TABLE_C | SELECT
SCHEMA2.TABLE_D | SELECT
SCHEMA2.TABLE_E | SELECT

INSERT的输出:

SCHEMA1.TABLE_A | INSERT
SCHEMA1.TABLE_B | INSERT
SCHEMA2.TABLE_C | INSERT
SCHEMA5.TABLE_C | SELECT

答案 1 :(得分:0)

所有操作的简单命令:

cat your_sql_script.sql | tr -d "\n" | tr ";" "\n" | sed 's/$/;/'

现在您可以grep任何sql命令。
这是您删除粘贴的输出:

DELETE FROM SCHEMA3.TABLE_K WHERE K.COLUMN_KK = 'A'
DELETE FROM SCHEMA1.TABLE_A A INNER JOIN SCHEMA2.TABLE_C C ON C.COLUMN_XX = A.COLUMN_XX WHERE A.COLUMN_AA = 'A'     

也许对您有帮助:

cat SELECT.sql | tr -d "\n" | tr ";" "\n" | sed 's/$/;/' | awk '{print $3,"|",$1}'     

输出:

SCHEMA3.TABLE_K | DELETE
SCHEMA1.TABLE_A | DELETE