---
title: "一.访问oracle之一，检索单行数据"
author: "springking"
date: "2008-07-04T09:00:15Z"
tags: []
slug: "oracle"
draft: false
lastmod: "2008-07-04T09:00:15Z"
id: 211314
url: "https://springking.iteye.com/blog/211314"
---

在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例外。