2013-07-02 58 views
0

我嘗試將Oracle SELECT命令的輸出保存到bash變量中。 我試過以下行,但它沒有工作真的很好...如何將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是正確的事做...

+0

是否'ORA-00947:沒有足夠的價值'給你一個提示? – devnull

回答

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" 
+1

here-doc的括號必須放在下一行,否則,你可能會得到一個像warning一樣的錯誤:here-document在行尾nnn由文件結尾分隔(想要'EOF') – Ali