-- Create table
create table EMP
(
empno NUMBER(4) not null,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
)
create or replace procedure firstP(name in varchar2) is
/*這里name為的參數,in為輸入,varchar2為類型*/
begin
/* dbms_output.put_line(); 相當輸出到控制臺上,這樣我們一個簡單的存儲過程就完成啦
記住一句話的結束使用分號結束,存儲過程寫完一定要執行
將它保存到數據庫中 (F8)快捷鍵,或者點擊左上角執行*/
dbms_output.put_line('我的名字叫'||name);/*dbms_output.put_line相當于JAVA中的System.out.println("我的名字叫"+name);*/
end firstP;
下面我們要對剛剛寫過的存儲過程進行測試,我們開啟Test Window這個窗口
-- Created on 2018/12/30 星期日 by ADMINISTRATOR
declare
-- Local variables here
/*測試名稱 名稱類型 使用 := 給參數賦值,在多說一句,分號結束本句*/
name2 varchar2(64):='數據庫';
begin
-- Test statements here
firstp(name2);
end;
我們打開DBMS Output就可以看到執行的存儲過程啦。
存儲過程IF判斷
create or replace procedure isifp(age in number) is
/*存儲過程if判斷以then開始,以end if; 結束*/
begin
if (age > 30) then
dbms_output.put_line('我已經超過30歲了');
else
if (age 10) then
dbms_output.put_line('我還是個兒童');
else
dbms_output.put_line('我正在奮斗時期');
end if;
end if;
end;
存儲過程輸出
create or replace procedure inandout(name in varchar2, age in number,outp out varchar2) is
/*in 代表輸入,out 代表輸出*/
begin
outp:='my name is '|| name ||',my age is '||age;/*相當于JAVA中的return outp,但是請注意,存儲過程中可以return多個值*/
end inandout;
測試輸出代碼
-- Created on 2018/12/30 星期日 by ADMINISTRATOR
declare
-- Local variables here
name varchar2(64):='數據庫';
age number:=06;
out_p varchar2(64);
begin
-- Test statements here
inandout(name,age,outp=>:out_p);
/*這里的outp是存儲過程中的輸出參數,out_p是在測試中使用的別名*/
end;
返回游標
create or replace procedure sysrefcursor(id in number, columnss out sys_refcursor) as
/*columnss out sys_refcursor 為輸出游標*/
begin
open columnss for
select * from emp where empno=id;
end;
測試游標
第一種測試方法
-- Created on 2018/12/30 星期日 by ADMINISTRATOR
declare
-- Local variables here
cursor ee is select * from emp where empno=7934;
begin
-- Test statements here
for e in ee loop
dbms_output.put_line('deptno:'||e.deptno);
end loop;
end;
輸出結果如下:
第二種測試方法
-- Created on 2018/12/30 星期日 by ADMINISTRATOR
declare
-- Local variables here
cursor ee is select * from emp where empno=7934;
cur ee % rowtype;
begin
-- Test statements here
open ee;
loop
fetch ee into cur;
exit when ee%notfound;
dbms_output.put_line('name:'||cur.ename);
end loop;
close ee;
end;
上面測試結果僅僅返回一條數據。下面我來演示返回多條數據的情況。
首先請看我表中的數據
有兩個job中內容為CLERK的數據。
-- Created on 2018/12/30 星期日 by ADMINISTRATOR
declare
-- Local variables here
cursor ee is select * from emp where job='CLERK';
begin
-- Test statements here
for e in ee loop
dbms_output.put_line('deptno:'||e.deptno);
end loop;
end;
游標返回多條數據。
由于對于初學者來說,游標可能不是很容易理解,下面我用JAVA語言來描述一下。
我們在java程序中寫條件查詢的時候,返回出來的數據是List泛型>。這個操作相當于游標,說白了就是個查詢而已(大家不要誤認為就這么一句簡單的SQL為什么要用游標,因為只是方便讀者學習游標罷了,具體業務具體分析,請不要抬杠哦)
當我們要使用list中的數據時,我們使用循環調用某一條數據時,是不是就要用實體類對象點get字段??梢岳斫鉃閒or e in ee loop dbms_output.put_line('deptno:'||e.deptno); end loop;
這里面的e.deptno。
獲取table中的column
create or replace procedure intop(id in number, print2 out varchar2) as
e_name varchar2(64);
begin
select ename into e_name from emp where empno = id;
if e_name ='ALLEN' then
dbms_output.put_line(e_name);
print2:='my name is '||e_name;
else if e_name ='SMITH' then
print2:='打印sql'||e_name;
else
print2:='打印其他';
end if;
end if;
end intop;
稍微復雜一點存儲過程
由于朋友這里有個需求需要用存儲過程,進而更新一下博客。
首先我們先創建一張表
-- Create table
create table CLASSES
(
id NUMBER not null,
name VARCHAR2(14),
classesc VARCHAR2(10),
seq NUMBER(5)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table CLASSES
add constraint PK_CLASSES primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
下面我們創建一個序列
-- Create sequence
create sequence SEQ_CLASSES
minvalue 1
maxvalue 9999999999999999999999999999
start with 2
increment by 1
cache 20;
下面創建存儲過程,寫的亂一些,希望不要介意
create or replace procedure proclasses(Names in varchar2,
classescs in varchar) as
/*在我們創建存儲過程的時候as其實是is*/
id number;/*設置變量名稱*/
c number;
seq number;
begin
select SEQ_CLASSES.nextval into id from dual;/*獲取下一個序列,使用into賦值給id這個變量名稱*/
dbms_output.put_line('classescs=' || classescs);/*打印而已*/
select count(*) into c from Classes where classesc = classescs;/*條件判斷,classesc=進來的變量*/
if (c > 0) then/*當數量大于0時*/
select max(seq) + 1 into seq from Classes where classesc = classescs;
dbms_output.put_line('第一個seq' || seq);
else
if (c = 0) then
seq := 0;/*如果查詢出來的數量為0的時候,我們賦值seq變量為0*/
dbms_output.put_line('c=0的時候seq' || seq);
end if;
end if;
insert into classes
(id, name, classesc, seq)
values
(id, names, classescs, seq);
/*insert插入這個不用多說了,大家都明白;注意的是我們insert之后一定要提交。
不然數據沒有持久化到數據庫,這個insert沒有任何意義了*/
end proclasses;
下面我們來調用這個存儲過程
-- Created on 2019/1/7 星期一 by ADMINISTRATOR
declare
-- Local variables here
names varchar2(32):='曉明';
classescs varchar2(32):='一班';
begin
-- Test statements here
proclasses(names,classescs);
end;