如何将SELECT中的SELECT放入bash变量?

时间:2013-07-02 09:09:57

标签: oracle bash variables

我尝试将Oracle SELECT命令的输出保存到bash变量中。 我尝试了以下几行,但效果不好......

ACCESS_SQL=`{
    sqlplus << EOF
    ${USER}/${PASSWORD}@DB
    set head off;
    set feedback off;
    set pagesize 5000;
    set linesize 30000;
    set serveroutput on;
    DECLARE
    data varchar(5000);
    BEGIN
    select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG into data from uam.access_list where USER_ID='${USER_ID}';
    dbms_output.put_line(data);
    END;
    /
    exit;
EOF
    }`

我得到的错误陈述是:

SQL> SQL> SQL> SQL> SQL> SQL>   2    3    4    5    6    7      select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG into data from uam.access_list where USER_ID='PZ230';
                                                                                                                *
ERROR at line 4:
ORA-06550: line 4, column 110:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 4, column 2:
PL/SQL: SQL Statement ignored

我想知道使用varchar是否正确...

1 个答案:

答案 0 :(得分:2)

您不需要select into变量,然后使用dbms_output.put_line将其打印出来。 (无论如何,您的select into语句无法正常工作,因为您无法在单个数据变量中选择多个列。)

相反,这样做:

data=$(sqlplus -S ${USER}/${PASSWORD} << EOF
  set head off
  set feedback off
  set pagesize 5000
  set linesize 30000
  select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG from uam.access_list where USER_ID='${USER_ID}';
  exit
EOF)

echo "$data"