Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2012
    Posts
    8

    Unanswered: How to establish a DB2 connection for a while loop in Unix shell scritping

    Hi ,
    I have to create a db2 connection for the while loop in shell scripting.
    Below is the connection for DB2 I have to establish
    connect to TABLESCHEMA user $USERID using $PASSWORD

    Below is the while loop.
    while read TABLE; do
    db2 LOAD CLIENT FROM
    $DIRECTORY/$TABLE.ixf OF ixf INSERT INTO TABLENAME NONRECOVERABLE INDEXING MODE REBUILD ALLOW NO ACCESS;
    done < $input_file

    Please help me..

  2. #2
    Join Date
    Mar 2003
    Posts
    280
    Quote Originally Posted by vikyalex4 View Post
    Hi ,
    I have to create a db2 connection for the while loop in shell scripting.
    Below is the connection for DB2 I have to establish
    connect to TABLESCHEMA user $USERID using $PASSWORD

    Below is the while loop.
    while read TABLE; do
    db2 LOAD CLIENT FROM
    $DIRECTORY/$TABLE.ixf OF ixf INSERT INTO TABLENAME NONRECOVERABLE INDEXING MODE REBUILD ALLOW NO ACCESS;
    done < $input_file

    Please help me..
    Here's an alternative approach

    for t in `cat $input_file`; do
    db2 load client from $dir/$t of ixf insert into ... # not sure how your table name is constructed
    done
    --
    Lennart

  3. #3
    Join Date
    Apr 2012
    Posts
    8
    But can i create db2 connection on top of the for loop ...will that work for entire for loop if i write
    connect to TABLESCHEMA user $USERID using $PASSWORD

  4. #4
    Join Date
    Mar 2003
    Posts
    280
    Quote Originally Posted by vikyalex4 View Post
    But can i create db2 connection on top of the for loop ...will that work for entire for loop if i write
    connect to TABLESCHEMA user $USERID using $PASSWORD
    That should work as long as you don't add additional pipes in the loop


    /Lennart
    --
    Lennart

  5. #5
    Join Date
    Apr 2012
    Posts
    8
    Why cant we use while loop ...does db connection wont work for while loop..?

    please let me know

  6. #6
    Join Date
    Mar 2003
    Posts
    280
    Quote Originally Posted by vikyalex4 View Post
    Why cant we use while loop ...does db connection wont work for while loop..?

    please let me know
    If it does not work, my guess would be that the problem is "< $input_file", not the while loop itself.
    --
    Lennart

  7. #7
    Join Date
    Apr 2012
    Posts
    8
    Hi Lennart,

    I tried with both and while and for loop using the below script

    cat> $loadsql << EOF
    connect to $schema user $userid using $password
    @
    EOF
    sh " db2 -td@ -f $loadsql"

    while read TABLE; do
    echo "$TABLE"
    db2 LOAD CLIENT FROM
    $DIRECTORY/$TABLE.ixf OF ixf INSERT INTO TABLENAME NONRECOVERABLE INDEXING MODE REBUILD ALLOW NO ACCESS;
    done < $input_file

    The error am getting is DB connection does not exits...

    I am not sure whether am doing writing it correct here.

    But if i place anything between the conenction statement and EOF then it works fine but with error...

    connect to $schema user $userid using $password
    @
    #something goes here like query
    EOF

  8. #8
    Join Date
    Mar 2003
    Posts
    280
    Quote Originally Posted by vikyalex4 View Post
    Hi Lennart,

    I tried with both and while and for loop using the below script

    cat> $loadsql << EOF
    connect to $schema user $userid using $password
    @
    EOF
    sh " db2 -td@ -f $loadsql"

    while read TABLE; do
    echo "$TABLE"
    db2 LOAD CLIENT FROM
    $DIRECTORY/$TABLE.ixf OF ixf INSERT INTO TABLENAME NONRECOVERABLE INDEXING MODE REBUILD ALLOW NO ACCESS;
    done < $input_file

    The error am getting is DB connection does not exits...

    I am not sure whether am doing writing it correct here.

    But if i place anything between the conenction statement and EOF then it works fine but with error...

    connect to $schema user $userid using $password
    @
    #something goes here like query
    EOF
    I'm not exactly sure what it is you are trying to do, but I'm thinking something like

    Code:
    #!/bin/sh
    
    [...]
    
    db2 "connect to $schema user $userid using $password"
    # add error handling, example:
    # [ $? -eq 0 ] || exit 1 
    
    for TABLE in `cat $input_file`; do
          db2 LOAD CLIENT FROM $DIRECTORY/$TABLE.ixf OF ixf INSERT INTO TABLENAME NONRECOVERABLE INDEXING MODE REBUILD ALLOW NO ACCESS
          
    done
    Are you trying to load the same table repeatable from different sources?
    --
    Lennart

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •