sqlplus statement from command line

后端 未结 4 1505
半阙折子戏
半阙折子戏 2020-12-02 09:03

Is it possible to do something like this?

$ sqlplus -s user/pass \"select 1 from dual\" or
$ echo \"select 1 from dual\" | sqlplus -s user/pas

相关标签:
4条回答
  • 2020-12-02 09:43

    My version

    $ sqlplus -s username/password@host:port/service <<< "select 1 from dual;"
    
    
             1
    ----------
             1
    

    EDIT:

    For multiline you can use this

    $ echo -e "select 1 from dual; \n select 2 from dual;" | sqlplus -s username/password@host:port/service
    
    
             1
    ----------
             1
    
    
             2
    ----------
             2
    
    0 讨论(0)
  • 2020-12-02 09:45

    I'm able to execute your exact query by just making sure there is a semicolon at the end of my select statement. (Output is actual, connection params removed.)

    echo "select 1 from dual;" | sqlplus -s username/password@host:1521/service 
    

    Output:

             1
    ----------
             1
    

    Note that is should matter but this is running on Mac OS X Snow Leopard and Oracle 11g.

    0 讨论(0)
  • I assume this is *nix?

    Use "here document":

    sqlplus -s user/pass <<+EOF
    select 1 from dual;
    +EOF
    

    EDIT: I should have tried your second example. It works, too (even in Windows, sans ticks):

    $ echo 'select 1 from dual;'|sqlplus -s user/pw
    
             1
    ----------
             1
    
    
    $
    
    0 讨论(0)
  • 2020-12-02 09:56

    Just be aware that on Unix/Linux your username/password can be seen by anyone that can run "ps -ef" command if you place it directly on the command line . Could be a big security issue (or turn into a big security issue).

    I usually recommend creating a file or using here document so you can protect the username/password from being viewed with "ps -ef" command in Unix/Linux. If the username/password is contained in a script file or sql file you can protect using appropriate user/group read permissions. Then you can keep the user/pass inside the file like this in a shell script:

    sqlplus -s /nolog <<EOF
    connect user/pass
    select blah;
    quit
    EOF
    
    0 讨论(0)
提交回复
热议问题