0

我尝试将 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 是否是正确的做法......

4

1 回答 1

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"
于 2013-07-02T09:27:41.223 回答