簡體   English   中英

條件程序 oracle Pl/sql

[英]Conditional Procedure oracle Pl/sql

我想創建一個具有以下條件的過程

$$
select 'Y'
     from dual
    where exists
             (select 'Y'
                from pa_expenditure_items_all paei, pa_expenditures_all pae
               where     paei.expenditure_id = pae.expenditure_id
                     and pae.incurred_by_person_id =&person_id
                     and paei.cost_distributed_flag = 'N')
$$

如果條件返回 Null 那么它應該顯示一條錯誤消息,然后是一個錯誤。 對於錯誤消息,我正在使用 dbms_output.put_line,對於錯誤,我正在使用異常,那么任何人都可以幫助我解決這些問題

我正在嘗試這些

 create or replace procedure validate_terminate
declare
l_yes out varchar2 (1);
null_found exception;
is
begin
   select 'Y'
     into l_yes
     from dual
    where exists
             (select 'Y'
                from pa_expenditure_items_all paei, pa_expenditures_all pae
               where     paei.expenditure_id = pae.expenditure_id
                     and pae.incurred_by_person_id =&person_id
                     and paei.cost_distributed_flag = 'N');

   if l_yes is null
   then
      dbms_output_put_line ('Condition is not met'); 
   raise null_found;
   end if;
   exception 
   when null_found then 
     dbms_output_put_line('CANNOT PROCESS TERMINATE');
end;

聲明過程時使用IS而不是DECLARE - 不要同時使用兩者。

您無需檢查l_yes是否為 null - 如果您的 select 返回 0 行,它將自動引發NO_DATA_FOUND異常。 抓住那個。

您不需要第二次 select(來自雙重)。 第一個 select 工作正常。

聲明中的l_yes之后不要out 這只是l_yes varchar2(1); .

它是dbms_output.put_line

使用綁定變量:person_id而不是替換變量&person_id

set define off
create or replace procedure validate_terminate
is
   l_yes varchar2(1);
begin
   select 'Y'
     into l_yes
     from dual
    where exists
             (select 'Y'
                from pa_expenditure_items_all paei, pa_expenditures_all pae
               where     paei.expenditure_id = pae.expenditure_id
                     and pae.incurred_by_person_id = :person_id
                     and paei.cost_distributed_flag = 'N');

   exception 
   when NO_DATA_FOUND then
     dbms_output.put_line ('Condition is not met');  
     dbms_output.put_line('CANNOT PROCESS TERMINATE');
     raise;
end;
/

您的變量聲明和異常處理不正確。

試試下面的代碼:

create or replace procedure validate_terminate
declare
l_yes varchar2(1);
--null_found exception;
is
begin
   select 'Y'
     into l_yes
     from pa_expenditure_items_all paei, pa_expenditures_all pae
   where paei.expenditure_id = pae.expenditure_id
     and pae.incurred_by_person_id =&person_id
     and paei.cost_distributed_flag = 'N';
   exception 
   when no_data_found then 
     dbms_output_put_line ('Condition is not met'); 
     dbms_output_put_line('CANNOT PROCESS TERMINATE');
end;
/

干杯!!

PL/SQL 不是交互式編程語言。 是實現SQL及相關代碼實現其他客戶端調用的工具。 是那些處理用戶輸入和其他 UI 事物的客戶端。

因此,編寫程序的慣用方式是將 PERSON_ID 作為參數傳遞....

create or replace procedure validate_terminate
   (p_person_id in pa_expenditure_items_all.incurred_by_person_id%type ) 
is
  l_yes out varchar2 (1);
  null_found exception;
is
begin
   select 'Y'
   into l_yes
   from dual
   where exists
             (select 'Y'
                from pa_expenditure_items_all paei
                     ,pa_expenditures_all pae
               where paei.expenditure_id = pae.expenditure_id
                 and pae.incurred_by_person_id =p_person_id
                 and paei.cost_distributed_flag = 'N');

   if l_yes is null
   then
      dbms_output_put_line ('Condition is not met'); 
   raise null_found;
   end if;
exception 
   when null_found then 
     dbms_output_put_line('CANNOT PROCESS TERMINATE');
end;

然后你像這樣調用這個過程(假設你想使用 SQL*Plus,正如你選擇的符號所暗示的那樣):

exec validate_terminate(&person_id)

注意我假設 PERSON_ID 是數字; 如果它是一個字符串,你需要引用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM