日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

PL/SQL编程基本概念

發布時間:2023/12/3 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PL/SQL编程基本概念 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/* =============================================================================pl/sql編程 =============================================================================*/--pl/sql塊的結構 declare --聲明部門:在此聲明pl/sql用到的變量、類型以及游標,以及局部的存儲過程和函數 begin--執行部分:過程及sql語句,即程序的組成部分 exception --異常處理部分:錯誤處理 end;--例子 create table toys ( id number(20), name varchar2(50), price number(5,2), sal_date date ) insert into toys values(1,'張三',525,sysdate) insert into toys values(2,'李四',525,'2016-05-06'); select * from toys;declare v_name varchar2(20);v_price number; beginselect name,price into v_name,v_price from toys where id=1;dbms_output.put_line('名字:'||v_name||',價格'||v_price); end; 10/* type和rowtype ------------------------------------------------- */ declare -- v_name varchar2(25);-- v_name1 toys.name%type; --返回一個v_name1 toys%rowtype; --返回多個e_ronull exception;--聲明異常 begin select * into v_name1 from toys where id=1;dbms_output.put_line('名字:'||v_name1.name); exception when dlp_val_on_index thendbms_output.put_line('將重復鍵插入id列'); end;--稅點 declare v_start constant number:=3500; --聲明常量 begin --sql語句 end--常量和變量的聲明變量名稱 pl/sql的數據類型(大小):=init_value; eg:variable_name constant data_type:=value;--應用實例 declare v_ename varchar2(20);v_rate number(7,2);c_rate_incr constant number(7,2):=1.10; begin--方法一:通過select into 賦值select ename,sal* c_rate_incr into v_ename,v_rate from employee where empno='7788';--方法二:通過賦值操作符“:=”給變量賦值v_ename:='scott'; end;--使用序列賦值v_no:=emp_seq.nextval;----------------------實例2--------------------------------- --根據員工編號查詢員工信息 declare v_empno employee.empno%type:=4;v_rec employee%rowtype; beginselect * into v_rec from employee where empno=v_empno;dbms_output.put_line('姓名:'||v_rec.ename||'工資:'||v_rec.sal||'工作時間:'||v_rec.hiredate);end; --==執行成功之后,輸出:姓名:張四工資:10000工作時間:2017-02-02 00:00:00/* ----------------------pl/sql控制語句-------------------------------- */ --if的語法 if <布爾表達式> thenpl/sql和sql語句 end if; ------------------------ if<布爾表達式> thenpl/sql和sql語句 else其他語句 end if; ------------------------- if <布爾表達式> thenpl/sql語句和sql語句 elsif <其他布爾表達式> then其他語句 elsif <其他布爾表達式> then其他語句 else其他語句 end if;----注意:是elsif 不是elseif------------------------------case的語法-------------------------- -------格式一------ case 條件表達式when 條件表達式結果1 then語句段1when 條件表達式結果2 then語句段2when 條件表達式結果n then語句段n[else語句段] end case; -------格式二------ casewhen 條件表達式1 then語句段1when 條件表達式2 then語句段2when 條件表達式n then語句段n else 語句段 end case;------------------------------循環控制-------------------------- loop 要執行的語句;exit when <條件語句> --條件滿足時,退出循環語句 end loop;---while循環語句的語法 while <布爾表達式> loop 要執行的語句; end loop; --for循環語句的語法 for 循環計數器 in [reverse] 下限 ...上限 loop要執行的語句 end loop;------------------------------實例3------------------------- declare v_counter number:=5; begindbms_output.put_line('v_counter的當前值為:'||v_counter);if v_counter>=10 thennull;--為了使語法變得有意義,去掉null會報語法錯誤elsev_counter:=v_counter+10;dbms_output.put_line('v_counter 的改變值為:'||v_counter);end if;end;--========執行成功之后輸出:v_counter的當前值為:5 v_counter 的改變值為:15/*=======================異常處理機制=============================== */--語法 begin sequence_of_statements; exception when <exception_name> thensequence_of_statements;when others thensequence_of_statements; end;----------------------------實例4------------------------------------ --查詢編號為7788的雇員的福利補助(comm列) declare v_comm employee.comm%type;e_comm_is_null exception ;--定義異常類型變量 begin select comm into v_comm from employee where empno=7788;if v_comm is null thenraise e_comm_is_null;end if; exception when no_data_found thendbms_output.put_line('雇員不存在!錯誤為:'||sqlcode||sqlerrm);when e_comm_is_null thendbms_output.put_line('該雇員無補助');when others then dbms_output.put_line('出現其他異常!');end; ----================測試運行結果:雇員不存在!錯誤為:100ORA-01403: 未找到任何數據--自定義異常 raise_application_error(error_number,error_message);--實例 declare ....begin ....if v_com is null thenraise_application_error(-20001,'該雇員無補助');end if; end;/* ====================================顯示游標================================ */ --1.聲明游標 cursor cursor_name [(parameter [,parameter]...)] [return return_type] is select_statement; --2.打開游標 open cursor_name[(parameters)]; --3.提取游標 fetch cursor_name into variables; --4.關閉游標 close cursor_name;--------------------實例6------------------------ declare name employee.ename%type;sal employee.sal%type; --定義兩個變量來存放ename和sal的內容cursor emp_cursor is select ename,sal from employee; beginopen emp_cursor;loopfetch emp_cursor into name,sal;exit when emp_cursor%notfound;dbms_output.put_line('第'||emp_cursor%rowcount||'個雇員:'||name|| 'oooo' || sal);end loop;close emp_cursor; end;--===執行成功輸出: /* 第1個雇員:張一3000 第2個雇員:張二5000 第3個雇員:張三8000 第4個雇員:張四10000 第5個雇員:張五6300*/--使用顯示游標刪除或者更新 cursor cursor_name id select_statement for update [of columns]; --在使用for update 子句聲明游標時,可以使用下面語法更新行 update table_name set column_name=column_value where current of cursor_name;--根據編號查詢雇員的姓名 declare v_ename varchar2(20); beginselect ename into v_ename from employee where empno=&empno;dbms_output.put_line('雇員的名字是:'||v_ename); end;select * from employee;

總結

以上是生活随笔為你收集整理的PL/SQL编程基本概念的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。