-
的
--使用while循環(huán)打印數(shù)字的1-10
set serveroutput on
declare
--定義循環(huán)變量
pnum number := 1;
begin
?while pnumber <=10 loop
? dbms_out.put_line(pnum);
? pnum := pnum +1;
end loop;
end;
/
--使用loop循環(huán)打印1-10
set serveroutput on
declare
--定義循環(huán)變量
?pnum number :=1;
begin
? loop
? --退出條件:循環(huán)變量大于10
? ?exit when pnum >10;
? ?dbms_output.put_line(pnum);
? --循環(huán)變量+1
? pnum := pnum +1;
end loop;
end;
/
--使用for循環(huán)打印1-10
set servieroutput on
declare?
--定義循環(huán)變量
begin
?for pnum in 1..10 loop
? ? ?dbms_output.line(pnum);
? ? ?pnum :=pnum +1;
end loop;
end;
/
查看全部 -
IF 語句
判斷用戶從鍵盤上輸入的輸入數(shù)字
如何使用if語句
接受一個鍵盤輸入(字符串)
set serveroutput
--接受一個鍵盤輸入
--num:地址值,含義是:在該地址上保存了輸入的值
accept num prompt '請輸入一個數(shù)字';
declare
--定義變量保存變量用戶從鍵盤輸入的數(shù)字
pnum number := #
begin
--執(zhí)行if語句進行條件判斷
if pnum = 0 then dbms_output.put_line('您水乳的數(shù)字是0');
? elsif pnum = 1 then dbms_output.put_line('您輸入的是1');
? elsif pnum = 2 then dbms_output.put_line('您輸入的是2');
? else dbms_output.put_line('其他數(shù)字')
end;
/
查看全部 -
引用型變量
舉例:
my_name emp.ename%type
引用型變量
set serveroutput on
declare
? ? --定義引用類型變量:查詢并打印7839的姓名和薪水
--pename varchar2(20)
--psal? ? ? ?number
pename emp.ename%type;
psal emp.sal%type
begin
?select ename,sal into pename,psal from emp where empno = 7839'
打印姓名和薪水
dbms_output.put_line(pename||'的薪水是'||psal);
end;
記錄型變量
舉例:
emp_rec emp%rowtype
記錄型變量分量的引用
emp_rec.ename := 'adams';
--使用記錄型變量,查詢并打印7839的姓名和薪水
set serveroutput on
declare
--定義記錄型變量:注意代表一樣
emp_rec emp%rowtype
begin
--得到7839
select *into emp_rec from emp where empno = 7839;
--打印姓名和薪水
dbms_out.put_line(emp_rec.ename||'的薪水是'||emp_rec.sal);
end;
/
查看全部 -
declare
????????說明部分(變量說明,光標申明,)
begin
? ? ? ? 語句序列(DML)
exception
? ? ? ? 例外處理語句
end;
/
--------------------------------------
定義基本變量
類型:char,varchar2,date,number,boolean,long
舉例:var1? ?char(15)
? ? married boolean? :=true
? ?psal? number(7,2)
----------------------------------------
--使用基本變量類型
declare
?--定義基本變量類型
--基本數(shù)據(jù)類型
pnumber number(7,2)
--字符串變量
pname varchar2(2);
--日期變量
pdate date
bebin
?????pnumber :=1
? ? ?dbms_output._line(pname);
? ??
? ? ?pname := 'Tom';
? ? ?dbms_output.put_line(pname);
? ? ?pdate := sysdate;
? ? ?dbms_output.putline(pdate);
? ? ?--計算明天的日期
‘ dbms_output.put_line(pdate+1);
查看全部 -
基本變量類型
查看全部 -
操作數(shù)據(jù)庫效率最高
查看全部 -
PLSQL程序設計
查看全部 -
declare? ?
?begin
end;
/
查看全部 -
/*
SQL語句
1.有哪些部門
? select deptno from dept; -->光標 -->循環(huán) -->退出條件:notfound
2.部門中員工的薪水
? select sal from emp where deptno = ? -->帶一個參數(shù)的光標-->循環(huán)-->退出條件:notfound
變量: 1.初始值 2.如何得到每個段的員工人數(shù)
count1 number;
count2 number;
count3 number;
每個部門的工資總額:
saltotal number;
1.SELECT sum(sal) into saltotal from emp where deptno = ???
2.累計??
*/
set serveroutput ON
DECLARE
?--部門的光標
?CURSOR? cdept is SELECT deptno from dept;
?pdeptno? dept.deptno%type;
?
?--部門中員工的薪水
?CURSOR? cemp(dno NUMBER) is SELECT sal from emp where deptno = dno;
?psal? ? emp.sal%type;
?
?--員工人數(shù)
?count1 number;
?count2 number;
?count3 number;
?
?--每個部門的工資總額:
?saltotal number;
BEGIN
?--打開部門光標
?open cdept;
?
?LOOP
? fetch cdept into pdeptno;
? EXIT when cdept%notfound;
??
? --初始化工作
? count1 :=0; count2:= 0; count3 := 0;
? --得到部門的工資總額
? --1.SELECT sum(sal) into saltotal from emp where deptno = ???
? --2.saltotal:=0;
? SELECT sum(sal) into saltotal from emp where deptno = pdeptno;
? --取部門中員工的薪水
? open cemp(pdeptno);
? ?LOOP
? ? --取一個員工的薪水
fetch cemp into psal;
EXIT WHEN cemp%notfound;
--saltotal:=saltotal+psal;
--判斷薪水范圍
if psal < 3000 then count1:=count1+1;
? ELSIF psal >= 3000 and psal < 6000 THEN count2:=count2+1;
? ELSE count3:=count3+1;
end if;
??
? ?end loop;?
? --關閉員工光標? ?
? close cemp;
? --保存當前部門的結果
? insert into msg VALUES( pdeptno,count1,count2,count3,NVL(saltotal,0) );
??
?end LOOP;
?
?--關閉部門光標
?close cdept;
?
?dbms_output.put_line('統(tǒng)計完成');
end;
/
查看全部 -
/*
SQL語句
select empno,sal from emp order by sal;
-->光標-->循環(huán)-->退出條件:1.工資總額大于5W,2%notfound
變量:1.初始值 2.如何得到
漲工資的人數(shù):
countEmp number := 0;
漲后的工資總量:
salTotal number;
1.select sum(sal) into salTotal from emp;
2.漲后的工資總額=漲前的工資總額+sal*0.1
*/
set serveroutput ON
DECLARE
?--定義光標
?CURSOR cemp is select empno,sal from emp order by sal;
?pempno emp.empno%TYPE;
?psal? ?emp.sal%TYPE;
?--漲工資的人數(shù)
?countEmp NUMBER :=0;
?
?--漲后的工資總額:
?salTotal number;
?
BEGIN
?--得到工資總額的
?select sum(sal) into salTotal from emp;
?
?--打開光標
?OPEN cemp;
?
?LOOP
? --1.工資總額大于5W
? exit when salTotal > 50000;
? --取一個員工漲工資
? FETCH cemp into pempno,psal;
? --2.notfound
? exit when cemp%notfound;
? --3.如果漲工資后總額超過5W
? exit when salTotal + psal*0.1 > 50000;
? --漲工資
? update emp set sal=sal*1.1 where empno = pempno;
? --人數(shù)+1
? countEmp := countEmp + 1;
? --2.漲后的工資總額=漲前的工資總額+sal*0.1
? salTotal := salTotal + psal*0.1;
??
?end loop;
?
?--關閉光標
?CLOSE cemp;
?
end;
/
查看全部 -
/*
SQL語句
select to_char(hiredate,'yyyy') from emp;
-->光標-->循環(huán)-->退出條件:notfound
變量:1.初始值,2.如何得到每年入職的員工人數(shù)
count80 number := 0;
count81 number := 0;
count82 number := 0;
count87 number := 0;
*/
set serveroutput on
declare
?--定義光標
?cursor c_emp is select to_char(hiredate,'yyyy') from emp;
?p_hiredate varchar2(4);
?--每年入職的員工人數(shù)
?count80 number := 0;
?count81 number := 0;
?count82 number := 0;
?count87 number := 0;
begin
?--打開光標
?open c_emp;
?
?loop?
? --取出一個員工的入職年份
? fetch c_emp into p_hiredate;
? exit when c_emp%notfound;
??
? --判斷入職年份
? if p_hiredate = '1980' then count80 :=count80+1;
? ?elsif p_hiredate = '1981' then count81 :=count81+1;
? ?elsif p_hiredate = '1982' then count82 :=count82+1;
? ?else count87 := count87+1;
? end if
??
?end loop;
?
?--輸出結果
?dbms_output.put_line('Total'||(count80+count81+count82+count87));
?dbms_output.put_line('1980'||count80);
?dbms_output.put_line('1981'||count81);
?dbms_output.put_line('1982'||count82);
?dbms_output.put_line('1987'||count87);
?--關閉光標
?close c_emp;
end;
/
查看全部 -
PL/SQL的程序結構
declare?
????說明部分(變量說明、光標申明、例外說明)
begin
????語句序列(DML語句)
exception
????例外處理語句
end;
/
搜索
復制
查看全部 -
什么是PL/SQL程序
PL/SQL(Procedure Language/SQL)
PLSQL是Oracle對sql語言的過程化擴展,指在SQL命令語言中增加了過程處理語句(如分支,循環(huán)等),使SQL語言具有過程處理能力。
搜索
復制
查看全部 -
打印Hello World
注意:如果要在屏幕上輸出信息,需要將serveroutput開關打開
set serveroutput on
declare
--說明部分
begin
--程序
dbms_output.put_line('Hello World');
end;
/
搜索
復制
查看全部 -
drop table sc;
drop table course;
drop table student;
drop table teacher;
drop table dep;
drop table msg1;
create table dep(
dno number(2),
dname? varchar2(30),
director number(4),
tel varchar(8)
);
create table teacher(
tno number(4),
tname? varchar2(10),
title? varchar2(20),
hiredate date,
sal number(7,2),
bonus number(7,2),
mgr number(4),
deptno number(2)
);
create table student(
sno number(6),
sname? varchar2(8),
sex? varchar2(2),
birth? date,
passwd varchar2(8),
dno number(2)
);
create table course(
cno varchar2(8),
cname? varchar2(20),
credit? number(1),
ctime? number(2),
quota? number(3)
);
create table sc(
sno number(6),
cno varchar2(8),
grade number(3)
);
-- 創(chuàng)建用于統(tǒng)計數(shù)據(jù)
create table msg1
(coursename varchar2(20),
dname varchar2(20),
count1 number,
count2 number,
count3 number,
avggrade number);
alter table dep add(constraint pk_deptno primary key(dno));
alter table dep add(constraint dno_number_check check(dno>=10 and dno<=50));
alter table dep modify(tel default 62795032);
alter table student add(constraint pk_sno primary key(sno));
alter table student add(constraint sex_check check(sex='男' or sex='女'));
alter table student modify(birth default sysdate);
alter table course add(constraint pk_cno primary key(cno));
alter table sc add(constraint pk_key primary key(cno,sno));
alter table teacher add(constraint pk_tno primary key(tno));
alter table sc add(foreign key(cno) references course(cno));
alter table sc add(foreign key(sno) references student(sno));
alter table student add(foreign key(dno) references dep(dno));
alter table teacher add(foreign key(deptno) references dep(dno));
insert into dep values(10,'計算機學院',9469,'62785234');
insert into dep values(20,'自動化學院',9581,'62775234');
insert into dep values(30,'無線電學院',9791,'62778932');
insert into dep values(40,'信息管理學院',9611,'62785520');
insert into dep values(50,'微納電子學院',2031,'62797686');
insert into teacher values(9468,'CHARLES','PROFESSOR','17-12月-17',8000,1000,null,10);
insert into teacher values(9469,'SMITH','PROFESSOR','17-12月-17',5000,1000,9468,10);
insert into teacher values(9470,'ALLEN','ASSOCIATE PROFESSOR','20-2月-16',4200,500,9469,10);
insert into teacher values(9471,'WARD','LECTURER','22-2月-17',3000,300,9469,10);
insert into teacher values(9581,'JONES','PROFESSOR','2-4月-2016',6500,1000,9468,20);
insert into teacher values(9582,'MARTIN','ASSOCIATE PROFESSOR','28-9月-2018',4000,800,9581,20);
insert into teacher values(9583,'BLAKE','LECTURER','1-5月-2019',3000,300,9581,20);
insert into teacher values(9791,'CLAKE','PROFESSOR','9-1月-2016',5500,null,9468,30);
insert into teacher values(9792,'SCOTT','ASSOCIATE PROFESSOR','09-12月-17',4500,null,9791,30);
insert into teacher values(9793,'BAGGY','LECTURER','17-11月-2017',3000,null,9791,30);
insert into teacher values(9611,'TURNER','PROFESSOR','8-9月-2018',6000,1000,9468,40);
insert into teacher values(9612,'ADAMS','ASSOCIATE PROFESSOR','12-1月-17',4800,800,9611,40);
insert into teacher values(9613,'JAMES','LECTURER','3-12月-19',2800,200,9611,40);
insert into teacher values(2031,'FORD','PROFESSOR','3-12月-18',5500,null,9468,50);
insert into teacher values(2032,'MILLER','ASSOCIATE PROFESSOR','23-1月-2018',4300,null,2031,50);
insert into teacher values(2033,'MIGEAL','LECTURER','23-1月-2019',2900,null,2031,50);
insert into teacher values(2034,'PEGGY','LECTURER','23-1月-2020',2800,null,2031,50);
-- 一定要提交,否則sqldeveloper工具查看不到數(shù)據(jù),sqlplus可以看到數(shù)據(jù)
commit;
查看全部 -
sqlplus如何導入sql數(shù)據(jù)的
@D:\mukewang\student.sql
查看全部 -
右擊scott,刷新,可以看到新建的表
表數(shù)據(jù) 全選 點叉 點執(zhí)行,可以刪除數(shù)據(jù)
查看全部 -
?create table msg(
?deptno number,
?count1 number,
?count2 number,
?count3 number,
?saltotal number);
set serveroutput on
declare
? ? --獲取所有部門
? ? cursor c_dept is select deptno from dept;
? ? --各部門編號
? ? p_dno dept.deptno%type;
? ? --各部門總金額
? ? p_totalsal number;
? ? --各工資分段人數(shù):
? ? num1 number;
? ? num2 number;
? ? num3 number;
? ? --定義一個游標存放該部門下所有員工(帶參數(shù))
? ? cursor c_emp(dno number) is select sal from emp where deptno = dno;
? ? --員工的薪水
? ? p_sal number;
begin
? ? --打開部門游標
? ? open c_dept;
? ? loop --部門循環(huán)
? ? ? ? fetch c_dept into p_dno;
? ? ? ? exit when c_dept%notfound;
? ? ? ??
? ? ? ? --初始化變量:
? ? ? ? p_totalsal:=0;
? ? ? ? num1:=0;
? ? ? ? num2:=0;
? ? ? ? num3:=0;
? ??
? ? ? ? --獲取本部門下所有員工,打開員工游標
? ? ? ? open c_emp(p_dno);
? ? ? ? ? ? loop --員工循環(huán)
? ? ? ? ? ? ? ? fetch c_emp into p_sal;
? ? ? ? ? ? ? ? exit when c_emp%notfound;
? ? ? ? ? ? ? ? if p_sal <3000 then num1:=num1+1;
? ? ? ? ? ? ? ? elsif p_sal >=3000 and p_sal<=6000 then num2:=num2+1;
? ? ? ? ? ? ? ? elsif p_sal>6000 then num3:=num3+1;
? ? ? ? ? ? ? ? end if;
? ? ? ? ? ? ? ? --獲取總金額
? ? ? ? ? ? ? ? p_totalsal:=p_totalsal+p_sal;
? ? ? ? ? ? end loop;
? ? ? ? ? close c_emp;
? ? ? ? ??
? ? ? ? ? --保存統(tǒng)計結果到msg
? ? ? ? ? insert into msg values(p_dno,num1,num2,num3,p_totalsal);
? ? ? end loop;
? ? ? close c_dept;
? ? ? commit;
? ? ? dbms_output.put_line('統(tǒng)計完成');
end;
/
查看全部 -
set serveroutput on
declare
? ? --定義光標
? ? cursor emp_cursor is select empno,sal from emp order by sal;
? ? --定義變量
? ? v_empno emp.empno%type;
? ? v_sal emp.sal%type;
? ? --漲工資的人數(shù)
? ? v_countemp number := 0;
? ? --漲工資的總額
? ? v_totalsal number;
begin
? ? rollback;
? ? --為漲工資總額賦值初始值
? ? select sum(sal) into v_totalsal from emp;
? ? --打開光標
? ? open emp_cursor;
? ? loop
? ? ? ? fetch emp_cursor into v_empno,v_sal;
? ? ? ? exit when emp_cursor%notfound;
? ? ? ? if(v_totalsal + v_sal * 0.1) < 50000 then
? ? ? ? ? ? -- 漲工資
? ? ? ? ? ? update emp set sal = sal*1.1 where empno = v_empno;
? ? ? ? ? ? --計算人數(shù)
? ? ? ? ? ? v_countemp := v_countemp + 1;
? ? ? ? ? ? --計算漲后工資總額
? ? ? ? ? ? v_totalsal := v_totalsal + v_sal * 0.1;
? ? ? ? else exit;
? ? ? ? end if;
? ? end loop;
? ? --關閉光標
? ? close emp_cursor;
? ? commit;
? ? --輸出數(shù)據(jù)
? ? dbms_output.put_line('漲工資的人數(shù)有:'||v_countemp||'人');
? ? dbms_output.put_line('漲后工資總額:'||v_totalsal||'元');
? ??
end;
/
查看全部 -
/**
統(tǒng)計每年入職的員工
*/
set serveroutput on
declare
? ? -- 定義光標
? ? cursor cemp is select to_char(hiredate,'yyyy') from emp;
? ? phiredate varchar2(4);
? ? --每年入職的員工數(shù)
? ? count80 number := 0;
? ? count81 number := 0;
? ? count82 number := 0;
? ? count87 number := 0;
begin
? ? -- 打開光標
? ? open cemp;
? ??
? ? loop
? ? --取出一個員工的入職年份
? ? fetch cemp into phiredate;
? ? exit when cemp%notfound;
? ??
? ? ? ? --判斷入職年份
? ? ? ? if phiredate = '1980' then count80:=count80+1;
? ? ? ? ? ? elsif phiredate = '1981' then count81:=count81+1;
? ? ? ? ? ? elsif phiredate = '1982' then count82:=count82+1;
? ? ? ? ? ? else count87:=count87+1;
? ? ? ? end if;
? ? end loop;
? ? -- 關閉光標
? ? close cemp;
? ? dbms_output.put_line('Total:'||(count80+count81+count82+count87));
? ? dbms_output.put_line('1980:'||count80);
? ? dbms_output.put_line('1981:'||count81);
? ? dbms_output.put_line('1982:'||count82);
? ? dbms_output.put_line('1987:'||count87);
? ??
end;
/
查看全部 -
declare
? ? --定義光標,代表50號部門的員工姓名
? ? cursor cemp is select ename from emp where deptno=50;
? ? pename emp.ename%type;
? ??
? ? --自定義例外
? ? no_emp_found exception;
begin
? ? --打開光標
? ? open cemp;
? ??
? ? --直接取一個員工的姓名
? ? fetch cemp into pename;
? ? if cemp%notfound then
? ? ? ? --拋出例外,用raise拋出自定義例外
? ? ? ? raise no_emp_found;
? ? end if;
? ??
? ? --關閉光標--if給攔截了,執(zhí)行不到,
? ? --但oracle自動啟用pmon(process monitor),自動清理系統(tǒng)遺留的垃圾和資源
? ? close cemp;
exception
? ? when no_emp_found then dbms_output.put_line('沒有找到員工');
? ? when others then dbms_output.put_line('其他例外');
end;
/
查看全部 -
-- 系統(tǒng)例外:value_error算術或轉換錯誤
-- 字符強制賦值給了數(shù)字
set serveroutput on
declare
? ? --定義一個number類型的變量
? ? pnum number;
begin
? ? pnum := 'abc';
exception
? ? when value_error then dbms_output.put_line('算術或轉換錯誤');
? ? when others then dbms_output.put_line('其他例外');
end;
/
查看全部 -
--系統(tǒng)例外:被0除zero_divide
set serveroutput on
declare
? ? --定義一個基本變量
? ? pnum number;
begin
? ? pnum := 1/0;
exception
? ? when zero_divide then dbms_output.put_line('1:0不能做除數(shù)');
? ? ? ? ? ? ? ? ? ? ? ? dbms_output.put_line('2:0不能做除數(shù)');
? ? ? ? ? ? ? ? ? ? ? ? -- 為了演示上面的處理語句可以寫多行,這里多寫了一行
? ? when others then dbms_output.put_line('其他例外');
end;
/
查看全部 -
-- 系統(tǒng)例外:too_many_rows
set serveroutput on
declare
? ? --定義變量
? ? pename emp.ename%type;
? ??
begin
? ? --查詢所有10號部門的員工姓名
? ? select ename into pename from emp where deptno=10;
exception
? ? when too_many_rows then dbms_output.put_line('select into 匹配了多行--非法操作');
? ? when others then dbms_output.put_line('其他例外');
end;
/
查看全部 -
-- 系統(tǒng)例外:no_data_found
set serveroutput on
declare
? ? pename emp.ename%type;? ??
begin
? ? --查詢員工號1234員工姓名
? ? select ename into pename from emp where empno=1234;
? ? -- 例外程序捕獲,如果拋給數(shù)據(jù)庫,可能導致數(shù)據(jù)庫出錯
exception
? ? when no_data_found then dbms_output.put_line('沒有找到數(shù)據(jù)');
? ? when others then dbms_output.put_line('其他例外');
end;
/
查看全部 -
-- 查詢某個部門中的員工姓名
set serveroutput on
declare
? ? --定義帶參數(shù)的光標
? ? cursor cemp(dno number) is select ename from emp where deptno=dno;
? ? pename emp.ename%type;
begin
? ? --打開光標
? ? open cemp(10);
? ??
? ? loop
? ? -- 取出每個員工的姓名
? ? fetch cemp into pename;
? ? exit when cemp%notfound;
? ? dbms_output.put_line(pename);
? ??
? ? end loop;
? ??
? ? --關閉光標
? ? close cemp;
end;
/
查看全部 -
/**
1.光標的屬性
?%found %notfound
?%isopen 判斷光標是否打開
?%rowcount 影響行數(shù)---而非總行數(shù)
?2.光標數(shù)的限制--光標打開了一定要關閉-
? 光標打開超出配置極限會報錯;
? 默認情況下,oracel一個會話中最多可以打開300個光標
*/
set serveroutput on
declare
? ? --定義光標
? ? cursor cemp is select empno,empjob from emp;
? ? pempno emp.empno%type;
? ? pjob emp.empjob%type;
begin
? ? --打開光標
? ? open cemp;
? ??
? ? loop
? ? ? ? --取出一條記錄
? ? ? ? fetch cemp into pempno,pjob;
? ? ? ? exit when cemp%notfound;
? ? ? ??
? ? ? ? --打印rowcount的值
? ? ? ? dbms_output.put_line('rowcount:'||cemp%rowcount);
? ? end loop;
? ??
? ? --關閉光標
? ? close cemp;
end;
/
查看全部
舉報