connect to sqlplus only once without writing to a file in a loop

这一生的挚爱 提交于 2019-12-06 12:09:02

问题


I have a requirement for which I need to write a ksh script that reads command line parameters into arrays and creates DML statements to insert records into an oracle database. I've created a script as below to achieve this. However, the user invoking the script doesn't have permission to write into the directory where the script has to run. So, is there a way we can fire multiple inserts on the database without connecting to sqlplus multiple times within the loop and at the same time, NOT create temp sql file as below? Any ideas are highly appreciated. Thanks in advance!

i=0
while (( i<$src_tbl_cnt ))
do
echo "insert into temp_table values ('${src_tbl_arr[$i]}', ${ins_row_arr[$i]},  ${rej_row_arr[$i]});" >> temp_scrpt.sql
(( i+=1 ))
done
echo "commit; disc; quit" >> temp_scrpt.sql
sqlplus user/pass@db @ temp_scrpt.sql

回答1:


Just use the /tmp directory.

The /tmp directory is guaranteed to be present on any unix-family server. It is there precisely for needs like this. Definitely do something like add the current process ID in the file name so that multiple users don't step on each other. So the total name is something like /tmp/temp_$PID_scrpt.sql or the like.

When done, be sure to also delete that file--say, in a line right after the sqlplus call. Thus be sure to store the file name in a variable and delete what's in that variable.

It should go without saying, but in a well run shop: 1) The admins should have put more than enough space in /tmp, 2) All the users in the community should not be deleting other's files in /tmp or overloading it so it runs out of space. 3) The admins should setup a job that deletes files from /tmp after a certain age so that if your script fails before it deletes the temporary file, it won't be there forever.

So really, this answer is more about /tmp and managing it effectively--but that really is what you need. Using temporary files is a powerful technique, so your design is good. And the reality that users often won't have rights in a directory is common, so /tmp is your answer.




回答2:


Instead of creating a temporary file you can directly pipe the output of an input generating block into sqlplus, in your shell script.

Example:

{
  echo 'set auto off;'
  for ((i=0; i<100; i++)); do
    echo "insert into itest(i) values ($i);"
  done
  # echo 'rollback;' # for testing
  echo 'commit;'
} | sqlplus -S juser/secret@db > /dev/null

This works with Ksh 93 and Bash (perhaps even with Ksh 88 modulo the (( expression syntax).

The corresponding DDL statement for the test table:

create table itest ( i number(36) ) ;

PS: Btw, even when creating a temporary file is preferred - redirecting the output is way more efficient than doing an append-style redirect for each line, e.g.:

{ for ((i=0; i<100; i++)); do echo "line $i"; done; echo end; } > foo.tmp


来源:https://stackoverflow.com/questions/21273838/connect-to-sqlplus-only-once-without-writing-to-a-file-in-a-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!