2014-01-21 51 views
5

如果我運行下面的腳本,我得到錯誤 SP2-0552:未聲明綁定變量「OUTRES」。 那麼,如何定義綁定變量OUTRES和在哪裏定義?Shell腳本嵌入Oracle PL/SQL代碼定義綁定變量

#!/usr/bin/bash 
sqlplus -s scott/tiger << EOF 
declare ret varchar2(10):= '0'; 
begin 
    begin 
    insert into mytab(col1) values(1); 
    exception 
    when others then 
     ret:=ret||'1'; 
    end; 
    select ret into :OUTRES from dual; 
end; 
/
quit 
EOF 

回答

6

如果要聲明sqlplus中的綁定變量。使用VAR關鍵字。

sqlplus -s scott/tiger << EOF 
VAR OUTRES NUMBER; 
BEGIN 
    NULL; /* Your Statements */ 
END; 
/
EOF 

您也可以嘗試quit :OUTRES

quit :OUTRES 
EOF 
MYRESULT=$? 
echo $MYRESULT 

它輸出的返回狀態的UNIX

#!/usr/bin/bash 
sqlplus -s scott/tiger << EOF 
VAR OUTRES NUMBER; 
declare ret varchar2(10):= '0'; 
begin 
    begin 
    EXECUTE IMMEDIATE 'insert into mytab(col1) values(1)'; 
    exception 
    when others then 
     dbms_output.put_line(SQLERRM); 
     ret:=ret||'1'; 
    end; 
    :OUTRES := ret; 
end; 
/
quit :OUTRES 
EOF 
MYRESULT=$? 
echo $MYRESULT 
+0

謝謝你的回覆,但[馬赫什(http://stackoverflow.com/users/3093319/maheswaran-ravisankar)你能告訴我哪兒在上面的代碼,我應該設置VAR(綁定變量)? –

+0

順便說一下,如果在PL/SQL代碼塊中發生了例如無效的表名或列名稱的任何錯誤,則儘管將ret更改爲存儲除1之外的其他值,但始終返回的錯誤值爲1 –

+0

你應該在運行你的pl/sql塊之前聲明,並且在進入sqlplus之後。 –

0
#!/usr/bin/bash 
sqlplus -s scott/tiger << EOF 
declare 
ret varchar2(10):= '0'; 
OUTRES varchar2(10); 
begin 
    begin 
    insert into mytab(col1) values(1); 
    exception 
    when others then 
     ret:=ret||'1'; 
    end; 
    select ret into OUTRES from dual; 
    dbms_output.put_line('Value of OUTRES is' || OUTRES); 
end; 
/
quit 
EOF