oracle--預存程序--bai

來源:互聯網
上載者:User

標籤:int   pre   簡單   put   csharp   when   highlight   儲存   異常   

--1 無入參最簡單的預存程序create or replace procedure hello_proc as   v_name varchar2(20);   v_age number; begin   v_name:=‘bai‘;   v_age:=18;   dbms_output.put_line(‘你好‘||v_name||‘,你今年‘||v_age); end;--在plsql中調用beginhello_proc;end;/* 在command窗下運行set serverout on;exec system.hello_proc;*/--練習1create or replace procedure show_sum_procas v_i number:=‘1‘; v_sum number:=‘0‘;begin  loop    v_sum:=v_sum+v_i;    v_i:=v_i+1;      exit when v_i=201;    end loop;   dbms_output.put_line(v_sum);end;--2 有入參的預存程序create or replace procedure show_add_result(  i number default 10,   --預設值  j number default 20)as v_result number;begin v_result :=i+j; dbms_output.put_line(i||‘+‘||j||‘=‘||v_result);end;begin  show_add_result(‘2‘);end;select * from scott.emp;--經典例子  顯示指定工號的員工姓名和工資,如果沒有,則輸出該員工不存在  create or replace procedure show_emp_by_eno_proc(  v_eno scott.emp.empno%type:=‘7369‘)asv_ename varchar(20);v_sal number;begin  select ename,sal into v_ename,v_sal from scott.emp where empno=v_eno;  dbms_output.put_line(v_eno||‘對應的員工名‘||v_ename||‘,工資‘||v_sal); exception  when no_data_found then  dbms_output.put_line(‘找不到‘||v_eno||‘對應的員工‘);end;create or replace procedure show_emp_by_eno_procasbegin   dbms_output.put_line(‘測試重載‘);end;begin   show_emp_by_eno_proc();end;--根據姓名尋找員工create or replace procedure show_emp_by_ename_proc(  v_ename varchar2)asv_sal number;begin  select sal into v_sal from scott.emp where ename=v_ename;  dbms_output.put_line(v_ename||‘對應的員工工資‘||v_sal); exception  when no_data_found then  dbms_output.put_line(‘找不到‘||v_ename||‘對應的員工‘);  when too_many_rows then  dbms_output.put_line(v_ename||‘對應的員工超過1個‘);  when others then    dbms_output.put_line(‘尋找過程出現異常!‘);end;---create or replace procedure show_dname_by_eno_depno(  v_depno scott.dept.deptno%type)asv_dname varchar(20);begin  select dname into v_dname from scott.dept where deptno = v_depno;  dbms_output.put_line(v_depno||‘對應的部門名‘||v_dname); exception  when no_data_found then  dbms_output.put_line(‘找不到‘||v_depno||‘對應的部門‘);end;--3 有入參,有出參的預存程序輸入i,j,用result出參獲得相加的結果create or replace procedure get_add_result_proc(  i number,  j number,  result out number   --指定result為出參)asbegin  result:=i+j;end;--使用plsql調用預存程序declare result number;begin     get_add_result_proc(‘1‘,‘2‘,result);      dbms_output.put_line(result);end;--建立預存程序,獲得當前的日期和星期幾create or replace procedure get_date_and_day(   v_date out varchar2 ,   v_day out varchar2 )asbegin  select to_char(sysdate,‘dd‘),to_char(sysdate,‘day‘)  into v_date,v_day from dual;end;

  

oracle--預存程序--bai

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.