加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

一.访问oracle之一,检索单行数据

发布时间:2020-12-12 16:12:49 所属栏目:百科 来源:网络整理
导读:在plsql中可以通过select...into语句将检索到的数据存放在变量中,然后输出或处理该变量的数据。 注意:当在plsql块中直接使用select...into语句时,该语句必须返回1条数据,并且只能返回1条记录。 使用标量变量接收数据 变量的个数,顺序及数据类型必须匹配

在plsql中可以通过select...into语句将检索到的数据存放在变量中,然后输出或处理该变量的数据。
注意:当在plsql块中直接使用select...into语句时,该语句必须返回1条数据,并且只能返回1条记录。

使用标量变量接收数据
变量的个数,顺序及数据类型必须匹配。
例如:
declare
v_name emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal into v_name,v_sal
from emp
where emp.empno = &no;
dbms_output.put_line(v_name||'的工资是'||v_sal);
end;

使用记录变量接收数据
记录成员的个数必须与选择列表项的个数完全一致,并且数据类型要匹配。
例如:
declare
type emp_record_type is record(
name emp.ename%type,
sal emp.sal%type
);
emp_record emp_record_type;
begin
select ename,sal into emp_record
from emp
where emp.empno = &no;
dbms_output.put_line(emp_record.name||'的工资是'||emp_record.sal);
end;

嵌入select语句注意事项* NO_DATA_FOUND例外当select...into语句没有返回任何数据时,触发该列外。* TOO_MANY_ROWS例外当select...into语句返回多条数据时,触发该列外。* where子句注意事项在where子句中使用变量时,变量名不能与列名相同,否则会触发TOO_MANY_ROWS例外。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读