2016-07-27 82 views
-1

我正在嘗試編寫運行內部小型查詢的小型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://stackoverflow.com/questions/3434437/whats-the-most-efficient-way-to-check-if-a-record-exists-in-oracle – Rene

+0

@ mc88。請確保您發佈表DDL以及當您希望某人處理您的不工作代碼時。 – XING

+0

table_data是什麼結構?你想要什麼,如果查詢存在 - 選擇一些東西? –

回答

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; 

它的做工精細,並編制好。現在執行專家建議讓它工作正常。我沒有看着你的邏輯。我只是讓你的代碼編譯。