您的位置:首页 > 数据库 > Oracle

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

2016-07-29 00:00 232 查看
在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例外。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Oracle