---
title: "select into语句容易报错的处理方法"
author: "java1573"
date: "2009-06-10T20:28:43Z"
tags: []
slug: "select-into"
draft: false
lastmod: "2009-06-10T20:40:47Z"
id: 406228
url: "https://java1573.iteye.com/blog/406228"
---

说明：使用select...into语句时，如果查询返回的数据不是1行，就会报no\_data\_found或者to\_many\_rows两种异常。

案例：根据部门号，查询部门名称。

处理方法：巧用聚合函数处理

DECLARE

  v\_dname dept.dname%TYPE;

BEGIN

   SELECT MAX(dept.dname) INTO v\_dname

   FROM dept WHERE deptno=90;

   IF(v\_dname IS NULL)THEN

      dbms\_output.put\_line('此部门不存在！');

   ELSE

      dbms\_output.put\_line(v\_dname);

   END IF;

  

END;

 

说明：利用聚合函数一定会返回结果的特点， 避免了使用select...into语句容易抛出异常的问题。