设为首页 收藏本站
查看: 568|回复: 0

[经验分享] PL/SQL学习笔记一

[复制链接]

尚未签到

发表于 2016-11-10 08:33:55 | 显示全部楼层 |阅读模式
1、 PL/SQL的塊結構
Declare
/*
Declare section-PL/SQL variables,types,cursors,and local subprograms go here.
*/
Begin
/*
Executable section-procedural and SQL statements go here.
This is ths main section of the bloack and the only one that is required.
*/
Exception
/*
Exception-handling section-error-handing statements go here
*/
End;注: 每個塊的結尾都要用分號結束
如下
declare
fmajor varchar(50):='Chemistary';
ffirstName varchar(50):='Fff';
flastName varchar(50):='LB';
begin
update students
set major=fmajor
where first_name=ffirstName
and last_name=flastName;
if SQL%NOTFOUND THEN
insert into students
values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
end if;
end;

2、 錯誤處理
/**錯誤處理開始**/
Declare
v_ErrorCode number;         --code for the error
v_ErrorMsg varchar2(200);   --Message text for the error
v_CurrentUser varchar2(20) --Current database user
v_Information varchar2(100) --Information about the error
Begin
/*Code that processes some data here*/
Exception
when others then
--Assign values to the log variables,using built-in function
v_ErrorCode:=SQLCODE;
v_ErrorMsg:=SQLERRM;
v_currentUser:=USER;
V_Information:='Error encountered on' || to_char(SYSDATE) || ' by database user' || v_CurrentUser;
--Insert the log message into log_table.
Insert into log_table(code,message,infro) values(v_ErrorCode,v_ErrorMsg,v_ErrorInfomation);
end;
/**錯誤處理結束**/

3、變量及類型
自帶基本類型
用戶自定義類型:
              Declare
type t_StudentRecord is record(
FirstName varchar2(50);
LastName varchar2(50);
CurrentCredits number(3);
);定義:v_Student t_StudentRecord;
4、過程
1)這里先介紹一個錯誤過程,也是很多初學者都會犯的問題
錯誤過程 1:
CREATE or replace PROCEDURE changOrInsert AS
Declare              --<出錯誤之處。原因,受塊結構的影響>
fmajor students.major%type:='Chemistary';
ffirstName students.first_name%type:='Fff';
flastName students.last_name%type:='LB';
begin
update students set major=fmajor where first_name=ffirstName and last_name=flastName;   
end changOrInsert;      
錯誤過程 2:
CREATE or replace PROCEDURE changOrInsert AS
fmajor students.major%type:='Chemistary';
ffirstName students.first_name%type:='Fff';
flastName students.last_name%type:='LB';
begin
update students set major=fmajor where first_name=ffirstName and last_name=flastName;   
if SQL%NOTFOUND THEN
insert into students
values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
end if;
exception
;             --<問題處。原因: 受其它開發語言如JAVA的影響>
end changOrInsert;錯誤過程3:
create or replace procedure addNewColumn
as
firstID int:=10140;
beginMark int:=65;
loopTime int:=1;
begin--仔細看下面這一條詔句。沒錯呀! 真的嗎?( 就錯在這里)
alter table students add mark int;
--execute immediate 'alter table students add mark1 int';( 這句才是對的)
loop
update students set mark=beginMark where id=firstID;
firstID:=firstID+1;
beginMark:=beginMark+1;
loopTime:=loopTime+1;
exit when loopTime>20;
end loop;
end addNewColumn;
2)過程的基本結構:
Create [or replace] procedure procedure_name [parameter_list] as
/*Declarative section is here*/
Begin
/*Executable section is here*/
Exception
/*Exception is here*/
End [procedure_name];

3) 一個完整的過程
/*Create a Procedure*/
CREATE or replace PROCEDURE changOrInsert(
fmajor students.major%type,
ffirstName students.first_name%type,
flastName students.last_name%type) AS
v_ErrorCode number;
v_ErrorMsg varchar(200);
v_currentUser varchar(50);
v_Information varchar(500);
begin
update students set major=fmajor where first_name=ffirstName and last_name=flastName;
if SQL%NOTFOUND THEN     --if not found the record
insert into students --then insert a new one
values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
end if;
exception
--write exception into table log_table
when others then
--Assign values to the log variables,using built-in function
v_ErrorCode:=SQLCODE;
v_ErrorMsg:=SQLERRM;
v_currentUser:=USER;
v_Information:='Error encountered on' || to_char(SYSDATE) || ' by database user ' || v_CurrentUser;      
--Insert the log message into log_table.
Insert into log_table(code,message,info) values(v_ErrorCode,v_ErrorMsg,v_Information);
end changOrInsert;

4) 執行存儲過程
       begin
changOrInsert('Feng','LiBin','Computer');
end;
       而不是像在SQL SERVER中的Call來調用
5、函數
       --
6、包
1)寫的第一個包, 花了不少的檢查時間(有點類似于JAVA中的接口)
表的結構如下圖:
create or replace package manageStu as
procedure addStu(firstname students.first_name%type,
lastname students.last_name%type,
majorm students.major%type,
current_credits students.current_credits%type,
markm students.mark%type);
procedure delStu(stuID int);
procedure modifyStu(idm int,
firstname students.first_name%type,
lastname students.last_name%type,
majorm students.major%type,
current_creditsm students.current_credits%type,
markm students.major%type);
end manageStu;

create or replace package body manageStu as
procedure addStu(firstname students.first_name%type,
lastname students.last_name%type,
majorm students.major%type,
current_credits students.current_credits%type,
markm students.mark%type)
as
begin
insert into students values(student_sequence.NEXTVAL,
firstname,
lastname,
majorm,
current_credits,
markm);
end addStu;

    procedure delStu(stuID int)
as
begin
delete students where id=stuID;
end delStu;
procedure modifyStu(idm int,
firstname students.first_name%type,
lastname students.last_name%type,
majorm students.major%type,
current_creditsm students.current_credits%type,
markm students.major%type)
as
begin
update students set
first_name=firstname,
last_name=lastname,
major=majorm,
current_credits=current_creditsm,
mark=markm
where id=idm;
end modifyStu;
end manageStu;
2)執行:
    begin
manageStu.addstu('F1','LB','computer',12,90);
end;
3) 包內過程、方法可以重載
4) 包的初使化
         當第一次調用打包程序時,該包將進行初使化。也就是說將該包從硬盤讀入到內存并啟動調用的子程序的編譯代碼開始運行。這時,系統為該包中定義的所有變量分配內存單元。每個會話都有其打包變量的副本,以確保一包子程序的兩個對話使用不同的內存單元。
         在大多數情況下,初使化代碼要在包第一次初使化時運行。為了實現這種功能,我們可以在包體中所有對象之后加入一個初使化部分,其語法如下:
         CREATE OR REPLACE PACEAGE BODY package_name (IS|AS)
. . .
BEGIN
Initialization_code;
END [package_name]

7、取當前系統時間
       SELECT TO_CHAR(SYSDATE,'SSSSS') FROM sys.dual;
8、更改表結構
       ALTER TABLE STU MODIFY FIRST_NAME VARCHAR(100);
       注: 一定是MODIFY而不是sql server中的ALTER

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-298213-1-1.html 上篇帖子: 【转】sql 关联更新 下篇帖子: SQL执行过程原理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表