简体   繁体   中英

SQL query not being executed from bash script

I have the following function currently, I know the outputted query works when I run it manually through the GUI SQL Client, but it doesnt work from Bash... I tried everything I could think of and googled with what keywords I could think about..

AssignChunksToDocs()
{
TEST="set heading off;
        set trim on;
        DECLARE
        i number;

        BEGIN
                i := 1;


                while i<=$CHUNK_AMOUNT LOOP
                  UPDATE TABLE T SET T.CHUNK_NUM = i
                  WHERE T.CHUNK_NUM IS NULL
                  AND ROWNUM <=$DOCS_PER_CHUNK;
                  i := i+1;
                END LOOP;
        i := i-1;
        -- Deal with the remainder
        UPDATE TABLE T SET T.CHUNK_NUM = i
        WHERE T.CHUNK_NUM IS NULL;

        end;"

        sqlplus -s $CONSTR<<ENDOFSQL
        set heading off;
        set trim on;
        DECLARE
        i number;

        BEGIN
                i := 1;


                while i<=$CHUNK_AMOUNT LOOP
                  UPDATE TABLE T SET T.CHUNK_NUM = i
                  WHERE T.CHUNK_NUM IS NULL
                  AND ROWNUM <=$DOCS_PER_CHUNK;
                  i := i+1;
                END LOOP;
        i := i-1;
        -- Deal with the remainder
        UPDATE TABLE T SET T.CHUNK_NUM = i
        WHERE T.CHUNK_NUM IS NULL;

        end;
ENDOFSQL

ERRORCODE=$?
if [ $ERRORCODE != 0 ]
then
  echo "********************"
  echo "ERROR: The SQL Plus Command Failed. ErrorCode: $ERRORCODE"
else
  echo "********************"
  echo "SQL Plus Successfully Ran. ErrorCode: $ERRORCODE"
fi

}

When I run it (with -x in shebang), I get the following snippet:

+ AssignChunksToDocs
+ TEST='set heading off;
        set trim on;
        DECLARE
        i number;

        BEGIN
                i := 1;


                while i<=
        64 LOOP
                  UPDATE TABLE T SET T.CHUNK_NUM = i
                  WHERE T.CHUNK_NUM IS NULL
                  AND ROWNUM <=3;
                  i := i+1;
                END LOOP;
        i := i-1;
        -- Deal with the remainder
        UPDATE TABLE T SET T.CHUNK_NUM = i
        WHERE T.CHUNK_NUM IS NULL;

        end;'
+ sqlplus -s dbuser/dbpass@hostname/sidname
+ ERRORCODE=0
+ '[' 0 '!=' 0 ']'
+ echo '********************'
********************
+ echo 'SQL Plus Successfully Ran. ErrorCode: 0'
SQL Plus Successfully Ran. ErrorCode: 0

Same happens for manual test of the query in terminal command line, followed by manual paste of << SQL ... SQL block.

Please help :)

The issue turned out to be a missing COMMIT; statement! In the future, in case of a weird situation like that with no errors but nothing apparent happening in the DB, ask the DBA to look for table locks!

Twice they were the cause of it not working for missing COMMIT; after every update statement.

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