Search code examples
arrayslinuxshelldb2ksh

Are there any ways to handle multiple elements at the same time in ksh?


I'm trying to execute delete SQL like below in ksh.

for ((i = 0; i < ${#array[@]}; i++))
do
  delete from TABLE1 where NAME in (${array[i]})
done

In this roop, I can delete row one by one. But what I want to is to delete multiple rows like 5 or 10 at the same time.

I have no idea, so I want your help.


Thank you, everyone! I finally could solve it by using below!

for ((i = 0; i < ${#array[@]}; i=i+5))
do
    echo ${array[@]:${i}:5} | sed -e s/^/\'/g | sed s/\\s/\',\'/g | sed s/$/\'/g | read array2
    delete from TABLE1 where NAME in (${array2})"
done

Solution

  • This an alternative way to do it (I don't have ksh, but it should be easy to adjust)

    #/bin/bash
    
    rc=0
    while [ $rc -eq 0 ]; do
        rc=$(db2 "delete from (select * from table1 where name in ... fetch first 10 rows only) dt")
    done
    

    As long as the derived table dt uniquely identifies a row in table1 you can delete from it.

    Another way is to stuff your parameters in a declared globally temporary table and use that in your delete. Can be handy if you have multiple predicates