0

I am having following sql script namely check_and_create_tbl.sql

DECLARE
t_count NUMBER;

BEGIN
  SELECT count(*) into t_count FROM all_objects WHERE object_name = '&1_EMPLOYEE' AND object_type = 'TABLE';

  if t_count <= 0 then
    execute immediate 'CREATE TABLE ' || &1 || '_EMPLOYEE (
      ID    NUMBER(3)
    , NAME  VARCHAR2(30) NOT NULL
    )';
  end if;
END;

Now I am calling above script in my bash program's function like below.

function exe_orcl {
  exe_command+=("/opt/bin/sqlplus -s <ORCL_USER>/<ORCL_PASS>@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=<ORCL_HOST>)(PORT=<ORCL_PORT>)))(CONNECT_DATA=(SERVICE_NAME=<ORCL_SNAME>))) ")

  while (( $# > 0 )); do
    exe_command+=("$1")
    shift 1
  done

  echo "SQLPLUS command  =  ${_cmd[@]}"

  "${exe_command[@]}" && { echo "SQLPLUS Success"; } || { echo "SQLPLUS Failed"; }
}

function exe_orcl_sql_file {
  sql_file=$1
  prefix="TBL_SALE"
  shift 1

  exe_orcl << EOF
  whenever oserror exit 9;
  whenever sqlerror exit SQL.SQLCODE;
  SET NEWPAGE NONE
  @${sql_file} ${prefix} ${@};
  exit
  EOF
}

Now in my bash program I try to execute sql file like below.

exe_orcl_sql_file "/tmp/check_and_create_tbl.sql" || { echo "SQLPLUS execution error"; }

I am seeing the log as below.

SQLPLUS command  =  /opt/bin/sqlplus -s XXXX/XXXX@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XXXX)(PORT=XXXX)))(CONNECT_DATA=(SERVICE_NAME=XXXX))) 

SQLPLUS Success

But I do not see the TBL_SALE_EMPLOYEE created in Oracle DB, please guide me as to what am i missing here?

1 Answer 1

1

You need to remember that the variable is input by sql*plus, you also have to realise that '_' is a valid part of a variable name so you need to clearly point out where the end should be.

DECLARE
t_count NUMBER;

BEGIN
  SELECT count(*) into t_count FROM all_objects WHERE object_name = '&1._EMPLOYEE' AND object_type = 'TABLE';

  if t_count <= 0 then
    execute immediate 'CREATE TABLE  &1._EMPLOYEE (
      ID    NUMBER(3)
    , NAME  VARCHAR2(30) NOT NULL
    )';
  end if;
END;
/
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, Andrew if I remove / from the last line, The table is not created. I do not want queries to be printed on console.
/ is to submit the statement to the database. Not exactly sure what you’re seeing and what you don’t want to see, but your -s switch in the sqlplus call should prevent anything from being written to console by sqlplus anyway.
also, in Both the cases when t_count=1 and when t_count=0, i see **execute immediate 'CREATE TABLE TBL_SALE_EMPLOYEE ** being printed on console, which led me to thinking that if condition is really working or not.
The client (sqlplus) knows nothing about what gets executed on the database unless there is some feedback (dbms_output etc), all it knows is that it submitted an anonymous block and it was either executed successfully or failed with an error. I’m not sure where the echoing of anything is coming from - I don’t think it’s SQL*plus, unless you have a (g)login.sql script doing set echo on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.