oracle 在 SQL*Plus 中声明绑定变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11737849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 04:22:46  来源:igfitidea点击:

Declare bind variables in SQL*Plus

oraclesqlplus

提问by made_in_india

I am using SQL*Plus. When I am using the below query, it is giving error

我正在使用 SQL*Plus。当我使用下面的查询时,它给出了错误

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

Query

询问

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

回答by Alex Poole

Not sure why you're using a PL/SQL block for that. You aren't using the idyou declare, and it would be better to give it a name different to the column name to avoid confusion.

不知道为什么要为此使用 PL/SQL 块。你没有使用id你声明的,最好给它一个与列名不同的名字以避免混淆。

You can declare a bind variable in SQL*Plus though, and select into that:

您可以在 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

Note the :before the references to the variables defined outside the block, indicating they are bind variables. l_idis declared inside the block so it does not have a preceding :.

注意在:块外定义的变量的引用之前,表明它们是绑定变量。l_id在块内声明,因此它没有前面的:.

In this case you could also define l_idoutside the block, and avoid PL/SQL while still using a bind variable for that:

在这种情况下,您还可以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;

Because the main query isn't PL/SQL any more (although the execis; that's just a shorthand for a one-line anonymous block), you don't need to select ... intoso you don't need to declare those variables.

因为主查询不再是 PL/SQL(尽管exec是;那只是单行匿名块的简写),select ... into因此您不需要声明这些变量。

回答by schurik

try this:

尝试这个:

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;