SQLPlus 检查 table 是否存在于 Shell 脚本中

SQLPlus Check if table exist in ShellScript

我正在尝试创建一个连接到 sqlplus 的 ShellScript 并验证 table 是否存在,如果不存在,脚本将创建它。

Table可以为空。

我试过这个:

#!/bin/bash

sqlplus -s user/pass> tabs << EOF


SET SERVEROUTPUT ON
SET FEEDBACK OFF
DECLARE
   e_not_exist EXCEPTION;
   PRAGMA EXCEPTION_INIT(e_not_exist, -942);
   tab_count NUMBER;
BEGIN
   SELECT COUNT(*) INTO tab_count
   FROM table_name;

   DBMS_OUTPUT.PUT_LINE(tab_count);
EXCEPTION
  when e_not_exist then
    dbms_output.put_line('Table or view does not exists');

END;
/
EXIT
EOF

tabcount=`cat tabs`
echo You have $tabcount tables.

输出:

尝试SELECT table_name FROM all_tables WHERE table_name = 'your_table_name'

all_tables 是一个字典视图,顾名思义。