I'm trying to create a ShellScript that connects to sqlplus and verify that a table exists or not, in case there is no exist the script will create it.
Table can be empty.
I tried this:
#!/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.
Ouput:
- You have 0 tables, when table exist. (empty)
- You have N tables, when table exist. (not empty)
- You have FROM table_name; ERROR at line 7: ORA-06550: line 7, column 9: PL/SQL: ORA-00942: table or view does not exist ORA-06550: line 6, column 4: PL/SQL: SQL Statement ignored tables.