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.
输出:
- 当 table 存在时,您有 0 个 table。 (空)
- 当 table 存在时,你有 N table 个。 (不为空)
- 你有 FROM table_name;第 7 行的错误:
ORA-06550:第 7 行,第 9 列:PL/SQL:ORA-00942:table 或视图
不存在 ORA-06550:第 6 行,第 4 列:PL/SQL:SQL 语句被忽略
tables.
尝试SELECT table_name FROM all_tables WHERE table_name = 'your_table_name'
all_tables 是一个字典视图,顾名思义。
我正在尝试创建一个连接到 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.
输出:
- 当 table 存在时,您有 0 个 table。 (空)
- 当 table 存在时,你有 N table 个。 (不为空)
- 你有 FROM table_name;第 7 行的错误: ORA-06550:第 7 行,第 9 列:PL/SQL:ORA-00942:table 或视图 不存在 ORA-06550:第 6 行,第 4 列:PL/SQL:SQL 语句被忽略 tables.
尝试SELECT table_name FROM all_tables WHERE table_name = 'your_table_name'
all_tables 是一个字典视图,顾名思义。