【Oracle】存儲過程基本語法

1、基本語法

CREATE OR REPLACE PROCEDURE 存儲過程名(param1 in type,param2 out type)

 /** AS和IS沒區別 **/
 IS

 變量1 類型(值範圍);

 變量2 類型(值範圍);  

 BEGIN

   select count(*) into 變量1 from 表名 where 列名=param1;

   if (判斷條件) then

     select 列名 into 變量2 from 表名 where 列名=param1;

     DBMS_OUTPUT.put_line('打印信息');

   Elseif (判斷條件) then

     dbms_output.put_line('打印信息');

   Else

     Raise 異常名 (NO_DATA_FOUND);

   End if;

 Exception

     When others then

       Rollback;   

 END;

2、基本使用

2.1、沒有參數的過程

create or replace procedure test_count 

 is 

  v_total int;

  v_date varchar(20);

 begin

   select count(*) into v_total from dual;

    select to_char(sysdate,'yyyy-mm-dd') into v_date from dual;     

    DBMS_OUTPUT.put_line('總人數:'||v_total);   

    DBMS_OUTPUT.put_line('當前時間:'||v_date);  

 end;

調用方法

begin

	test_count;

end;

2.2、僅帶傳入參數的過程

create or replace procedure test_count1(v_id in varchar2)

 as

 	v_name varchar(100);

 begin

  select c_name into v_name from tb_store where c_stono=v_id;

  DBMS_OUTPUT.put_line(v_id||'店的名稱爲:'||v_name);

  exception

   when no_data_found then dbms_output.put_line('no_data_found');

 end;

調用方法

begin

 test_count1(11910);

end;

2.3、僅帶輸出參數的過程

create or replace procedure test_count2(v_name out varchar2)

 is

 begin

    select c_name into v_name from tb_store where c_stono='1101';

  exception

   when no_data_found then dbms_output.put_line('no_data_found');

 end;

調用方法

declare

v_name varchar(200);

begin

  test_count2(v_name);

  dbms_output.put_line(v_name);

end;

2.4、帶輸入參數和輸出參數的存儲過程

create or replace procedure test_count3(v_id in int,v_name out varchar2) 

 as

 begin   

		select c_name into v_name from tb_store where c_stono=v_id;

		dbms_output.put_line(v_name);

  exception

		when no_data_found then dbms_output.put_line('no_data_found');

 end;

調用方法

declare

v_name varchar(200);

begin

  test_count3('1101',v_name);

end;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章