1樓:
HelloWorld.sql
set serveroutput on
declare
--變數的說明
begin
--程式體
--程式包package
dbms_output.put_line('Hello World');
end;
/--if語句:判斷使用者輸入的數字
set serveroutput on
/*1.提示資訊
2. 接收鍵盤輸入
num 是乙個位址值
SQL優化: num繫結變數(盡量使用繫結變數)
select * from emp where deptno=10; --> 執行計畫
select * from emp where deptno=20; --> 執行計畫
-->select * from emp where deptno=#
*/accept num prompt '請輸入乙個數字';
declare
--變數儲存輸入的數字
pnum number := #
begin
--判斷
if pnum = 0 then
dbms_output.put_line('您輸入的是0');
elsif pnum = 1 then
dbms_output.put_line('您輸入的是1');
elsif pnum = 2 then
dbms_output.put_line('您輸入的是2');
else
dbms_output.put_line('其他數字');
end if;
end;
/--帶引數的游標:查詢某個部門的員工姓名
set serveroutput on
declare
cursor cemp(pdno number) is select ename from emp where deptno=pdno;
pename emp.ename%type;
begin
open cemp(20);
loop
fetch cemp into pename;
exit when cemp%notfound;
dbms_output.put_line(pename);
end loop;
close cemp;
end;
--給員工漲工資Quattroporte1000 經理800 其他400
set serveroutput on
/*SQL> show parameters cursor
NAMETYPE VALUE
cursor_sharingstring EXACT
cursor_space_for_timeboolean FALSE
open_cursorsinteger 300
session_cached_cursorsinteger 20
*/declare
--游標代表員工
cursor cemp is select empno,empjob from emp;
pempno emp.empno%type;
pjob emp.empjob%type;
begin
rollback;
open cemp;
loop
fetch cemp into pempno,pjob;
exit when cemp%notfound;
--判斷
if pjob = 'PRESIDENT' then update emp set sal=sal+1000 where empno=pempno;
elsif pjob = 'MANAGER' then update emp set sal=sal+800 where empno=pempno;
else update emp set sal=sal+400 where empno=pempno;
end if;
end loop;
close cemp;
--提交: 隔離級別
commit;
dbms_output.put_line('完成');
end;
/--游標: 使用游標查詢員工姓名和工資,並列印
/*游標的屬性:
%isopen 是否被開啟
%rowcount 行數
%notfound 是否有值
*/set serveroutput on
declare
--游標
cursor cemp is select ename,sal from emp;
pename emp.ename%type;
psal emp.sal%type;
begin
open cemp;
loop
--從集合中取值
fetch cemp into pename,psal;
exit when cemp%notfound;
dbms_output.put_line(pename||'的薪水是'||psal);
end loop;
close cemp;
end;
/--記錄型變數查詢並列印7839的姓名和薪水
set serveroutput on
declare
--定義變數
emp_rec emp%rowtype;
begin
--得到一行
select * into emp_rec from emp where empno=7839;
dbms_output.put_line(emp_rec.ename||'的薪水是'||emp_rec.sal);
end;
/*例項1:統計每年入職的員工個數。
可能SQL:
select to_char(hiredate,'yyyy') from emp;
*/set serveroutput on
declare
cursor cemp is select to_char(hiredate,'yyyy') from emp;
phiredate varchar2(4);
--計數器
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;//*
為員工長工資。從最低工資調起每人長10%,但工資總額不能超過5萬元,
請計算長工資的人數和長工資後的工資總額,並輸出輸出長工資人數及工資總額。
可能的SQL:
員工: select empno,sal from emp order by sal;
長工資後的工資總額:1. 對sal進行累加: 新的工資總額=舊的工資 + sal*0.1;
2. sum(sal): 查詢資料庫
練習: 工資不能超過5w
*/set serveroutput on
declare
--員工
cursor cemp is select empno,sal from emp order by sal;
pempno emp.empno%type;
psal emp.sal%type;
--長工資的人數
countEmp number := 0;
--工資總額
salTotal number;
begin
--漲前工資總額
select sum(sal) into salTotal from emp;
open cemp;
loop
--工資總額》5w
exit when salTotal > 50000;
--取乙個員工
fetch cemp into pempno,psal;
exit when cemp%notfound;
--漲工資
update emp set sal=sal*1.1 where empno=pempno;
--人數
countEmp := countEmp +1;
--工資總額
salTotal := salTotal + psal * 0.1;
end loop;
close cemp;
commit;
--輸出
dbms_output.put_line('長工資的人數:'|| countEmp);
dbms_output.put_line('工資總額:'|| salTotal);
end;//*
用PL/SQL語言編寫一程式,實現按部門分段(6000以上、(6000,3000)、3000元以下)
統計各工資段的職工人數、以及各部門的工資總額(工資總額中不包括獎金)
SQL語句:
部門: select deptno from dept;
員工的工資: select sal from emp where deptno=???
工資總額: select sum(sal) from emp where deptno=???
*/set serveroutput on
declare
--部門
cursor cdept is select deptno from dept;
pdno dept.deptno%type;
--部門中的員工
cursor cemp(dno number) is select sal from emp where deptno=dno;
psal emp.sal%type;
--各個段的人數
count1 number;count2 number;count3 number;
--部門的工資總額
salTotal number;
begin
open cdept;
loop
--取部門
fetch cdept into pdno;
exit when cdept%notfound;
--初始化
count1 :=0;count2:=0;count3:=0;
select sum(sal) into salTotal from emp where deptno=pdno;
--取部門中的員工
open cemp(pdno);
loop
fetch cemp into psal;
exit when cemp%notfound;
--判斷
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 msg1 values(pdno,count1,count2,count3,nvl(salTotal,0));
end loop;
close cdept;
commit;
dbms_output.put_line('完成');
end;
/*Zero_Divide ( 被零除)
*/set serveroutput on
declare
pnum number;
begin
pnum := 1/0;
exception
when Zero_Divide then dbms_output.put_line('1: 0不能做被除數');
dbms_output.put_line('2: 0不能做被除數');
when Value_error then dbms_output.put_line('算術錯');
when others then dbms_output.put_line('其他例外');
end;
--迴圈: 列印1~10
set serveroutput on
declare
pnum number := 1;
begin
loop
--退出成立退出不成立迴圈
exit when pnum > 10;
--隱式轉換
dbms_output.put_line(pnum);
pnum := pnum + 1;
end loop;
end;
--引用型變數: 查詢並列印7839的姓名和薪水
set serveroutput on
declare
--定義變數
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;
--自定義例外: 查詢50號部門的員工姓名
set serveroutput on
declare
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 no_emp_found;
end if;
/*if cemp%isopen then
close no_emp_found;
end if;
*/close cemp;
exception
when no_emp_found then dbms_output.put_line('沒有找到員工');
when others then dbms_output.put_line('其他例外');
end;/
為什麼我努力了這麼久,還換不來乙個好結果?
紋鳳 你努力的喜歡乙個人?努力的做著乙份工作?努力的學習一項技能?什麼叫努力換不來乙個好結果?如果這就是你的努力,那其實你已經有了乙個好結果,起碼你感動了自己。求而不得是人生特別平常的過程,身陷囹圄只能靠自己爬出來,別氣餒,別灰心! 如是初衷 很多人或許都會問這個問題,為什麼我這麼努力還不能達到期望...
為什麼乙個漂亮女生要對我那麼好?她這麼做的動機是什麼?
犬次郎DDL 其實你真的想多了。很多女生主動要qq主動聯絡根本就不是喜歡,只是單純的交朋友而已,對別人主動示好也只是她們的一種社交方式而已,如果你還想更加明確的話你就殷勤一點主動約她單獨出去玩,如果她委婉的拒絕了你那就是沒戲,如果她出來了你就直接表白吧,看她一瞬間的表情我估計你就什麼都明白了。 我發...
暴雪為什麼不出乙個吃雞遊戲?
老夫在情 瀉藥個人觀點 首先說說為什麼是小工作室首先暴雪出品必須精品十年磨一劍的事情對於暴雪來說都是很隨意的那麼暴雪出品的遊戲肯定在遊戲內Bug的問題上會有很多任務作而吃雞其實一小部分的遊戲樂趣就來自於某些遊戲bug 比如被手雷炸死的屍體和船能被車推到內陸更不用說毀天滅地的平底鍋了 其次養蠱一類的遊...