2012-07-31 89 views
3

我正在使用SQL * Plus。當我使用下面的查詢,它給錯誤聲明SQL * Plus中的綁定變量

Error report: 
ORA-06550: line 4, column 1: 
PLS-00428: an INTO clause is expected in this SELECT statement 

查詢

declare 
id varchar2(80) :='test123'; 
begin 
select test_quote,test_id from order_link where id = 'test123'; 
end; 

回答

12

不知道爲什麼你正在使用PL/SQL塊那。您沒有使用您聲明的id,最好給它起一個與列名不同的名稱以避免混淆。

可以在SQL聲明綁定變量* Plus中,雖然,選擇成:

var l_test_quote varchar2(80); -- or whatever type/size you need 
var l_test_id varchar2(80); 

declare 
    l_id varchar2(80) :='test123'; 
begin 
    select test_quote, test_id 
    into :l_test_quote, :l_test_id 
    from order_link 
    where id = l_id; 
end; 
/

print l_test_quote 
print l_test_id 

注意引用塊外定義的變量前:,這表明它們是綁定變量。 l_id在塊內聲明,因此它沒有前面的:

在這種情況下也可以定義l_id塊之外,並且避免PL/SQL同時仍使用該綁定變量:

var l_id varchar2(80); 

exec :l_id := 'test123'; 

select test_quote, test_id 
from order_link 
where id = :l_id; 

由於主查詢不是PL/SQL任何更多(雖然exec是;這只是一個單行匿名塊的縮寫),所以您不需要select ... into,因此不需要聲明這些變量。

0

試試這個:

declare 
id varchar2(80) :='test123'; 
    v_test_quote order_link.test_quote%type; 
    v_test_id order_link.test_id%type; 
begin 
    select test_quote,test_id 
    into v_test_qoute, v_test_id 
    from order_link 
    where id = 'test123'; 
end;