简体   繁体   中英

Shell script to update row using SQL Plus

Okay, so I have a script that I'm trying to run that creates a file with an update statement and pipes that to sqlplus to execute. I know that the update statement is valid and it creates the file to send to sqlplus is correct because I have ran just that code in SQL Plus. It just doesn't work when I execute the script. My team and I have looked at this and have tried multiple things, but just can't figure out why it doesn't seem to be executing. Any help would be great. Thanks!

 # Update Cycle DUE_DATE_TIME field to target run date
   UPDATE_SQL="UPDATE CYCLE SET DUE_DATE_TIME = to_date('${TARGET_RUN_DATE}', 'YYYYMMDD') - 1"
   echo $UPDATE_SQL >> ${WORK_AREA}/update_cycle_table_${PROCESS_ID}.sql
    echo "${USER_NAME}@${DATABASE}
    set line 200
    start ${WORK_AREA}/update_cycle_table_${PROCESS_ID}.sql
    commit
    exit"|sqlplus -s > /dev/null
   if [[ $? -eq 0 ]]
   then
      print "Updated Cycle successfully\n\n"
   fi

Okay, so I resolved the issue by making it a bit simpler and instead of just writing it to a file and running that in SQLPlus, I just did the update within the pipe to SQLPlus. I tried to do that before and it wasn't working, but I guess whatever I did this time worked. Thanks everyone for your input!

You're missing a ; at the end of the update and after the commit . You should probably add something like :

spool ${WORK_AREA}/update_cycle_table_${PROCESS_ID}.log 

after the set line to get a log of what's not working.

I'd suggest this. (Note I typed this without access to ksh, so you might have to play with the code a bit to get it to work). I've never been a big fan of piping stuff into sqlplus. And the return from sqlplus is not very useful. The return only indicates if sqlplus succesfully ran, not any error condition with the underlying sql.

sqlplus -s /nolog <<EOD >> ${sql_local_output_file}
  connect ${connect_string}
  @${WORK_AREA}/update_cycle_table_${PROCESS_ID}.sql
  commit
EOD

if (!grep -q -e error ${sql_local_output_file}); then 
      print "Updated Cycle successfully\n\n"
else
      cat ${sql_local_output_file}
fi

Resolved by this code instead of the previous:

#Update cycle table with user input TARGET RUN DATE
  echo "${USER_NAME}@${DATABASE}
       set feedback off
       UPDATE CYCLE SET DUE_DATE_TIME = to_date('${TARGET_RUN_DATE}', 'YYYYMMDD') - 1;
       exit"|sqlplus -s >/dev/null
   if [[ $? -eq 0 ]]
   then
      print "Updated Cycle successfully\n\n"
   fi

It wasn't even reaching the code that was piped into SQLPlus for some reason, so I made it simpler and just did the above. Weird how this simpler code wouldn't work early. Must have overlooked something before. Thanks again everyone for looking into this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM