Oracle PL/SQL 編程基礎 執行個體

來源:互聯網
上載者:User

create table mytest(name varchar(20),password varchar(30));


create or replace procedure sp_pro2 is 
begin 
insert into mytest values('fc','123');
end;




查看錯誤資訊


show error


如何調用該過程:


1, exec 過程名 (參數,。。)
2.  call 過程名 (參數  )




set server output on


begin 
dbms_output.put_line('helloe');
end;


---------------------




declare 
  v_ename varchar(20);
begin 
    select ename into v_ename from emp where empno=&aa;
  dbms_output.put_line('姓名:'||v_ename);
  end;






  -----------
  create procedure sp_pro3 (spName varchar2,newSal number) is
  begin  
                          --執行部分
     update emp set sal=newSal where ename=spName;
     end;
     --dioyong 
     call sp_pro3('scott','8000');
 --------------函數 -------
 
    CREATE or replace  function sp_fun1(spName varchar2) return 
    number is yearsal number(7,2);
     begin select sal*12+nvl(comm,0) into yearsal from emp 
       where ename=spName;
       return yearsal;
       end;
    
    --diaoyong
    var abc number;
    call sp_fun1('SCOTT') into : abc;
    ---------------------------
    
   -------包------------由包規範和包體組成的
   --建立一個包sap_pack1 
   --聲明該包有一個過程和一個函數
   create or replace package sp_pack1 is
     procedure update_sal(spName varchar2,newsal number);
     function  annual_income(spName varchar2) return number;
     end;
     
     --建立包體---
         create or replace package body sp_pack1 is
          
         procedure update_sal(spName varchar2,newsal number) is
     begin  
     update emp set sal=newsal where ename=spName;
     end;
     
      function   annual_income(spName varchar2) return number
           is yearsal number(7,2);
     begin   select sal*12+nvl(comm,0) into yearsal from emp 
       where ename=spName;
       return yearsal;
    end;
    end;


 ---調用 ----
        call sp_pack1.update_sal('SCOTT',9000);










----------定義並使用變數---================================
     --  標量類型 scalar 
      v_ename varchar(10);
      v_sal  number(6,2):=5.4
      v_data date;
      v_valid  boolean not null default false;
                       ---輸入員工號,顯示 name sal tax
                       declare 
                         v_tax_rate number (3,2):=0.03;
                       -- v_ename varchar2(5);
                         v_ename  emp.ename%type;
                        -- v_sal  number(7,2);
                         v_sal  emp.sal%type;
                          v_tax_sal number (7,2);
                       begin  
                           select ename,sal into v_ename,v_sal from emp where empno=&no;
                           --計算所得稅 
                             v_tax_sal:=v_sal*v_tax_rate;
                           --輸出
                            dbms_output.put_line('name:'||v_ename||'工資'||v_sal  ||'交稅:'||v_tax_sal);
                           end;


--複合類型  composite
      ----pl/sql 記錄  類似於進階語言的結構體 
              --定義一個plsql 記錄類型  emp_record_type
                  declare
                type   emp_record_type is record (aname emp.ename%type,salary emp.sal%type,title emp.job%type);
                --定義了一個 變數 sp_record  這個變數的類型是emp_record_type                                                                           
                  sp_record emp_record_type;
               begin 
                 select ename,sal,job into  sp_record from  emp where empno=7788;
               dbms_output.put_line('員工名:'||sp_record.aname);
               end;
      --------pl/sql  表  相當於該機語言裡的數組 
            --定義一個plsql 表類型  emp_table_type   
                  declare 
                  type emp_table_type is table  of emp.ename%type index by  binary_integer;
                    sp_table emp_table_type;
                  begin 
                     select ename into  sp_table(0)  from emp where empno=7788;
                     dbms_output.put_line('員工名--》'||sp_table(0)); 
                  end;
--參照類型
        ---1遊標變數
               --執行個體一  輸入部門號,顯示所有的員工姓名和他的工資
                       declare
                             --定義遊標類型
                             type sp_emp_cursor is ref cursor;
                              --定義遊標變數
                             test_cursor sp_emp_cursor; 
                             --定義變數
                             v_ename emp.ename%type;
                             v_sal emp.sal%type;
                         begin 
                              --執行
                              --把test_cursor 和select 結合
                            open test_cursor for select ename,sal  from emp where deptno=&depno;
                            --迴圈取出
                            loop
                              fetch test_cursor into v_ename,v_sal;
                              --判斷是否test——cursor 為空白
                               exit when  test_cursor%notfound;
                              dbms_output.put_line('員工名->'||v_ename||'工資->'||v_sal); 
                            end loop;
                         end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.