在SQL * Plus中声明绑定变量 [英] Declare bind variables in SQL*Plus

查看:233
本文介绍了在SQL * Plus中声明绑定变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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;

推荐答案

不确定为什么要为此使用PL/SQL块.您不使用声明的id,最好给它一个与列名不同的名称,以免造成混淆.

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

不过,您可以在SQL * Plus中声明一个绑定变量,然后选择一个绑定变量:

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

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在块内声明,因此它没有前面的:.

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

在这种情况下,您还可以在块外部定义l_id,并避免在仍然使用绑定变量的情况下避免使用PL/SQL:

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

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,所以您不需要需要声明这些变量.

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

这篇关于在SQL * Plus中声明绑定变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆