Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    6

    Question Unanswered: syntax about db2 command after switching user

    when I run the following command, it returns '(' unexpected.
    Please tell me how can I deal with this?
    Thank you very much!

    $ su - USER -c db2 select * from TABLE where COL1=upper('x1')
    Last edited by raccoon; 02-19-12 at 21:57.

  2. #2
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    If you are using Linux or UNIX, surround the entire SQL statement in double quotes.
    M. A. Feldman
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows
    IBM Certified DBA on DB2 for z/OS and OS/390

  3. #3
    Join Date
    Sep 2011
    Posts
    6

    Red face

    Quote Originally Posted by Marcus_A View Post
    If you are using Linux or UNIX, surround the entire SQL statement in double quotes.
    I tried
    $ su - USER -c "db2 'select * from TABLE where COL1=upper('x1')'"

    and this time it returned

    "x1" is not valid in the context where it is used.

  4. #4
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    Try this:

    $ su - USER db2 -c "select * from TABLE where COL1=upper('x1')"

    or two separate commands:

    $ su - USER
    $ db2 -c "select * from TABLE where COL1=upper('x1')"
    M. A. Feldman
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows
    IBM Certified DBA on DB2 for z/OS and OS/390

  5. #5
    Join Date
    Sep 2011
    Posts
    6
    Quote Originally Posted by Marcus_A View Post
    Try this:

    $ su - USER db2 -c "select * from TABLE where COL1=upper('x1')"

    or two separate commands:

    $ su - USER
    $ db2 -c "select * from TABLE where COL1=upper('x1')"
    1. $ su - USER db2 -c "select * from TABLE where COL1=upper('x1')" ----> error

    and try: $ su - USER -c db2 "select * from TABLE where COL1=upper('x1')"
    result: '(' is not expected.

    2. $ su - USER
    $ db2 -c "select * from TABLE where COL1=upper('x1')"
    ----> I want to write this in a script.


    3. and I tried: $ su - USER -c db2 "select * from TABLE where COL1=upper\\(\\'x1\\'\\)" ----> OK!

    Thank you very much!!
    Last edited by raccoon; 02-20-12 at 00:09.

  6. #6
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    I don't understand what you are trying to do. Also, it would help to know DB2 version and OS you are using.

    When you switch users, I assume the new user would have to connect to DB2 before issuing the select statement.

    Not sure why you are using "-c" since autocommit is the default.

    If you want to put the following in a shell script, should be no problem

    Code:
    . $HOME/sqllib/db2profile (This initiates the db2 profile for shell script. Yes, there is a blank after the period)
    db2 "connect to sample"
    db2 -c "select * from TABLE where COL1=upper('x1')"
    M. A. Feldman
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows
    IBM Certified DBA on DB2 for z/OS and OS/390

  7. #7
    Join Date
    Sep 2011
    Posts
    6

    Smile

    -c is the option of su, not the db2 option.
    $ su - USER -c COMMAND

    I want to run the shell by root, and use the db2 command with INSTUSER.
    $ su - INSTUSER
    $ db2 "select * from TABLE where COL1=UPPER('x1')"
    and writing these in one shell can't make it out.

    so, it will be:
    $ su - INSTUSER -c db2 "select * from TABLE where COL1=UPPER\\(\\'x1\\'\\)"
    and otherwise it will return error.

    I am a new shell writer.....
    Thank you very much for helping me!!!

Posting Permissions

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