bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

oracle中存儲過程如何使用-創新互聯

今天就跟大家聊聊有關oracle中存儲過程如何使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:域名注冊、網頁空間、營銷軟件、網站建設、玉門網站維護、網站推廣。

一. 使用for循環游標:遍歷所有職位為經理的雇員

1. 定義游標(游標就是一個小集合)

2. 定義游標變量

3. 使用for循環游標

declare
  -- 定義游標c_job
  cursor c_job is
    select empno, ename, job, sal from emp where job = 'MANAGER';
    
  -- 定義游標變量c_row
  c_row c_job%rowtype;
begin
  -- 循環游標,用游標變量c_row存循環出的值
  for c_row in c_job loop
    dbms_output.put_line(c_row.empno || '-' || c_row.ename || '-' ||
                         c_row.job || '-' || c_row.sal);
  end loop;
end;

二. fetch游標:遍歷所有職位為經理的雇員

使用的時候必須明確的打開和關閉

declare
  --定義游標c_job
  cursor c_job is
    select empno, ename, job, sal from emp where job = 'MANAGER';

  --定義游標變量c_row
  c_row c_job%rowtype;
begin
  open c_job;
  loop
    --提取一行數據到c_row
    fetch c_job into c_row;
    
    --判讀是否提取到值,沒取到值就退出
    exit when c_job%notfound;
    dbms_output.put_line(c_row.empno || '-' || c_row.ename || '-' ||
                         c_row.job || '-' || c_row.sal);
  end loop;
  
  --關閉游標
  close c_job;
end;

三. 使用游標和while循環:遍歷所有部門的地理位置

--3,使用游標和while循環來顯示所有部門的的地理位置(用%found屬性)
declare
  --聲明游標
  cursor csr_TestWhile is select loc from dept;

  --指定行指針
  row_loc csr_TestWhile%rowtype;
begin
  open csr_TestWhile;
  --給第一行數據
  fetch csr_TestWhile into row_loc;
  
  --測試是否有數據,并執行循環
  while csr_TestWhile%found loop
    dbms_output.put_line('部門地點:' || row_loc.LOC);
    --給下一行數據
    fetch csr_TestWhile into row_loc;
  end loop;
  close csr_TestWhile;
end;

四. 帶參的游標:接受用戶輸入的部門編號

declare
  -- 帶參的游標
  cursor c_dept(p_deptNo number) is
    select * from emp where emp.deptno = p_deptNo;
    
  r_emp emp%rowtype;
begin
  for r_emp in c_dept(20) loop
    dbms_output.put_line('員工號:' || r_emp.EMPNO || '員工名:' 
                         || r_emp.ENAME || '工資:' || r_emp.SAL);
  end loop;
end;

五. 加鎖的游標:對所有的salesman增加傭金500

declare
  --查詢數據,加鎖(for update of)
  cursor csr_addComm(p_job nvarchar2) is
    select * from emp where job = p_job for update of comm;
  r_addComm emp%rowtype;
  commInfo  emp.comm%type;
begin
  for r_addComm in csr_addComm('SALESMAN') loop
    commInfo := r_addComm.comm + 500;
    
    --更新數據(where current of)
    update emp set comm = commInfo where current of csr_addComm;
  end loop;
end;

六. 使用計數器:找出兩個工作時間最長的員工

declare
  cursor crs_testComput is
    select * from emp order by hiredate asc;
    
  --計數器
  top_two      number := 2;
  r_testComput crs_testComput%rowtype;
begin
  open crs_testComput;
  fetch crs_testComput into r_testComput;
  while top_two > 0 loop
    dbms_output.put_line('員工姓名:' || r_testComput.ename ||
                         ' 工作時間:' || r_testComput.hiredate);
    --計速器減1
    top_two := top_two - 1;
    fetch crs_testComput into r_testComput;
  end loop;
  close crs_testComput;
end;

七. if/else判斷:對所有員工按基本薪水的20%加薪,如果增加的薪水大于300就取消加薪

declare
  cursor crs_upadateSal is
    select * from emp for update of sal;
  r_updateSal crs_upadateSal%rowtype;
  salAdd      emp.sal%type;
  salInfo     emp.sal%type;
begin
  for r_updateSal in crs_upadateSal loop
    salAdd := r_updateSal.sal * 0.2;
    if salAdd > 300 then
      salInfo := r_updateSal.sal;
      dbms_output.put_line(r_updateSal.ename || ':  加薪失敗。' ||
                           '薪水維持在:' || r_updateSal.sal);
    else
      salInfo := r_updateSal.sal + salAdd;
      dbms_output.put_line(r_updateSal.ENAME || ':  加薪成功.' ||
                           '薪水變為:' || salInfo);
    end if;
    update emp set sal = salInfo where current of crs_upadateSal;
  end loop;
end;

八. 使用case
when:按部門進行加薪

declare
  cursor crs_caseTest is
    select * from emp for update of sal;

  r_caseTest crs_caseTest%rowtype;
  salInfo    emp.sal%type;
begin
  for r_caseTest in crs_caseTest loop
    case
      when r_caseTest.deptno = 10 THEN
        salInfo := r_caseTest.sal * 1.05;
      when r_caseTest.deptno = 20 THEN
        salInfo := r_caseTest.sal * 1.1;
      when r_caseTest.deptno = 30 THEN
        salInfo := r_caseTest.sal * 1.15;
      when r_caseTest.deptno = 40 THEN
        salInfo := r_caseTest.sal * 1.2;
    end case;
    update emp set sal = salInfo where current of crs_caseTest;
  end loop;
end;

九. 異常處理:數據回滾

set serveroutput on;
declare
  d_name varchar2(20);
begin
  d_name := 'developer';
  
  savepoint A;
  insert into DEPT values (50, d_name, 'beijing');
  savepoint B;
  insert into DEPT values (40, d_name, 'shanghai');
  savepoint C;
  
  exception when others then
    dbms_output.put_line('error happens'); 
	  rollback to A;
  commit;
end;
/

十. 基本指令:

set serveroutput on size 1000000 format wrapped; --使DBMS_OUTPUT有效,并設置成較大buffer,防止"吃掉"最前面的空格
set linesize 256; --設置一行可以容納的字符數
set pagesize 50; --設置一頁有多少行數
set arraysize 5000; --設置來回數據顯示量,這個值會影響autotrace時一致性讀等數據
set newpage none; --頁和頁之間不設任何間隔
set long 5000; --LONG或CLOB顯示的長度
set trimspool on; --將SPOOL輸出中每行后面多余的空格去掉
set timing on; --設置查詢耗時
col plan_plus_exp format a120; --autotrace后explain plan output的格式
set termout off; --在屏幕上暫不顯示輸出的內容,為下面的設置sql做準備
alter session set nls_date_format='yyyy-mm-dd hh34:mi:ss'; --設置時間格式

小知識:

下面的語句一定要在Command Window里面才能打印出內容

oracle中存儲過程如何使用


set serveroutput on;
begin 
dbms_output.put_line('hello!');
end;
/

看完上述內容,你們對oracle中存儲過程如何使用有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創新互聯行業資訊頻道,感謝大家的支持。

網頁名稱:oracle中存儲過程如何使用-創新互聯
當前鏈接:http://vcdvsql.cn/article4/csshoe.html

成都網站建設公司_創新互聯,為您提供靜態網站品牌網站制作微信公眾號做網站響應式網站搜索引擎優化

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

搜索引擎優化