PL / SQL - 在找到并找不到记录时放置行

时间:2016-07-27 07:53:35

标签: sql oracle plsql

我正在尝试编写一个小型PL / SQL块,它在里面运行一个小查询。问题是我不想查看查询返回的全部数据,而是查看是否存在某些内容。我的块看起来像这样:

procedure check_data as
table_data varchar2;
BEGIN
      SELECT * into table_data FROM  (
     with temp_table  as ( select   a_number, a_group, a_date from table1
                     where    a_id in (15)
                     )
       SELECT b_city, b_district, b_nationality, b_age
         FROM table2 JOIN temp_table ON a_id=b_id 
        WHERE b_age>=10
        and b_age<23
                ORDER BY b_nationality DESC);

  IF SQL%FOUND THEN 
                raise_application_error(-20001,'OK, found something')
  else DBMS_OUTPUT.PUT_LINE ('found nothing!');
  end if;
  end;

一般都在努力宣布temp_table(我得到PLS-00201:标识符&#39; table_data&#39;必须声明)并将结果放在屏幕上。

我会感激任何提示。

1 个答案:

答案 0 :(得分:0)

立即尝试:

create  table table1( a_id number,a_number number, a_group varchar(10), a_date date) 

create table table2 (b_id number,b_city varchar(10), b_district varchar(10), b_nationality varchar(10), b_age number)

create or replace procedure check_data as
table_data varchar2(100);
BEGIN
      SELECT * 
      into table_data 
      FROM  (
      with temp_table  as ( select  a_id, a_number, a_group, a_date 
                            from  table1
                          where    a_id in (15)
                     )
       SELECT 1
         FROM table2 
       JOIN temp_table ON a_id = b_id 
        WHERE b_age>=10
        and b_age<23       
        ORDER BY b_nationality DESC)
       where  rownum < 2 ;

    IF SQL%FOUND THEN 
       raise_application_error(-20001,'OK, found something');
    Else 
    DBMS_OUTPUT.PUT_LINE ('found nothing!');
    End if;
  end;

它工作正常,编译得很好。现在实施专家建议,以使其正常工作。我不是在考虑你的逻辑。我只是编译你的代码。