PL/SQL - 同时找到记录并且找不到记录

问题描述:

我正在尝试编写运行内部小型查询的小型PL/SQL块。问题是,我不想看到查询返回的整个数据,但看到是否存在或不存在。我的块看起来是这样的: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:标识符“TABLE_DATA”必须申报),并在屏幕上放的效果。

我会很感激任何提示。

+4

http://*.com/questions/3434437/whats-the-most-efficient-way-to-check-if-a-record-exists-in-oracle – Rene

+0

@ mc88。请确保您发布表DDL以及当您希望某人处理您的不工作代码时。 – XING

+0

table_data是什么结构?你想要什么,如果查询存在 - 选择一些东西? –

现在试试这个:

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; 

它的做工精细,并编制好。现在执行专家建议让它工作正常。我没有看着你的逻辑。我只是让你的代码编译。