PLSQL Day4

--使用顯式游標更新行,對所有salesman增加500獎金:
declare
? cursor s_cursor is
? select * from emp?
? where job = 'SALESMAN'
? for update;
begin
? for e_s in s_cursor loop
? ? update emp set comm = nvl(comm,0)+500?
? ? where current of s_cursor;
? end loop;
end;

--3.定義游標:顯示所有部門編號與名稱,以及其所擁有的員工人數:
declare
? cursor i_cursor is
? select d.deptno,d.dname,count(e.empno) cnt
? from dept d ?left join emp e
? on d.deptno = e.deptno
? group by d.deptno,d.dname;
begin
? for e in i_cursor loop
? ? dbms_output.put_line(e.deptno||' '||e.dname||' '||e.cnt);
? end loop;
end;

--4.用游標屬性%rowcount實現輸出前十個員工的信息:

declare
? cursor a_cursor is
? select * from emp;
begin
? for e in a_cursor loop
? ? if a_cursor%rowcount < 11?
? ? ? ?then dbms_output.put_line(e.empno||' '||e.ename||' '||e.job
? ? ? ? ?||' '||e.mgr||' '||to_char(e.hiredate,'yyyy-MM-dd')
? ? ? ? ?||' '||e.sal||' '||e.comm
? ? ? ? ?||' '||e.deptno);
? ? end if;
? end loop;
end;
--5.通過使用游標來顯示dept表中的部門名稱,
--及其相應的員工列表(提示:可以使用雙重循環):
declare
? cursor a_cur is select * from dept;
? cursor b_cur is select * from emp;
begin
? for a in a_cur loop
? ? for b in b_cur loop
? ? ? if a.deptno=b.deptno then?
? ? ? ? dbms_output.put_line(a.dname||' '||b.empno||' '
? ? ? ? ||b.ename||' '||b.job||' '||b.mgr||
? ? ? ? ' '||to_char(b.hiredate,'yyyy-mm-dd')||' '
? ? ? ? ||b.sal||' '||b.comm);
? ? ? end if;
? ? end loop;
? end loop;
end;

--6.定義游標:接受一個部門號,從emp表中顯示該部門的所有雇員的姓名,工作和薪水:
declare
? cursor c_cur(dno number) is select * from emp where deptno = dno;
begin
? for e in c_cur(&部門號) loop
? ? dbms_output.put_line(e.ename||' '||e.job||' '||e.sal);
? end loop;
end;

--7.定義游標:將emp表中前5人的名字,及其工資等級(salgrade)顯示出來:
declare
? cursor d_cur is select e.ename,s.grade?
? from emp e join salgrade s
? on e.sal between s.losal and s.hisal;
begin
? for e in d_cur loop
? ? if d_cur%rowcount < 6
? ? ? then dbms_output.put_line(e.ename||' '||e.grade) ?; ?
? ? end if;
? end loop;
end;


--8.定義游標:在emp表中對所有雇員按他們基本薪水的10%給他們加薪,
--如果所增加后的薪水大于5000,則取消加薪:

declare
? cursor e_cur is select * from emp for update;
begin
? for e in e_cur loop ? ?
? ? e.sal := e.sal*1.1;
? ? if e.sal<=5000 then
? ? ? ?update emp set sal = sal*1.1 where current of e_cur;
? ? end if;
? end loop;
end;

select * from emp;
--9.按照salgrade表中的標準,給員工加薪,
--1:5%,2:4%,3:3%,4:2%,5:1%,
--并打印輸出每個人,加薪前后的工資:

declare
? cursor f_cur is?
? select e.empno,e.ename,e.sal,s.grade?
? from emp e join salgrade s
? on e.sal between s.losal and s.hisal
? for update;
? e_sal number;
begin
? for e in f_cur loop
? ? if e.grade = 1?
? ? then e_sal := e.sal*1.05 ?; ? ? ? ?
? ? elsif e.grade = 2?
? ? then e_sal := e.sal*1.04 ?; ?
? ? elsif e.grade = 3?
? ? then e_sal := e.sal*1.03 ?;?
? ? elsif e.grade = 4?
? ? then e_sal := e.sal*1.02 ?;
? ? else e_sal := e.sal*1.01 ?;?
? ? end if;
? ? dbms_output.put_line(e.ename||' - 前:'||e.sal||' ?后:'||e_sal);
? ? update emp set sal = e_sal ?
? ? where empno = e.empno;
? end loop;
end;

select * from emp;
select * from salgrade;


-----

declare
? cursor f_cur is select * from emp for update;
? cursor g_cur is select * from salgrade;
? e_sal number; ?
begin
? for f in f_cur loop
? ? for g in g_cur loop
? ? ? if f.sal between g.losal and g.hisal and g.grade = 1
? ? ? then e_sal := f.sal * 1.05;
? ? ? elsif f.sal between g.losal and g.hisal and g.grade = 2
? ? ? then e_sal := f.sal * 1.04; ??
? ? ? elsif f.sal between g.losal and g.hisal and g.grade = 3
? ? ? then e_sal := f.sal * 1.03; ?
? ? ? elsif f.sal between g.losal and g.hisal and g.grade = 4
? ? ? then e_sal := f.sal * 1.02; ??
? ? ? elsif f.sal between g.losal and g.hisal and g.grade = 5
? ? ? then e_sal := f.sal * 1.01;?
? ? ? end if;?
? ? end loop;
? ? dbms_output.put_line(f.ename||' - 前:'||f.sal||' ?后:'||e_sal);
? ? update emp set sal = e_sal ?where current of f_cur;
? end loop;
end;


--10.用游標獲取所有收入(sal+comm)超過2000的 salesman:

declare
? cursor h_cur is select * from emp where sal+nvl(comm,0)>2000 and job = 'SALESMAN';
begin
? for h in h_cur loop
? ? dbms_output.put_line(h.empno||' '
? ? ? ? ||h.ename||' '||h.job||' '||h.mgr||
? ? ? ? ' '||to_char(h.hiredate,'yyyy-mm-dd')||' '
? ? ? ? ||h.sal||' '||h.comm||' '||h.deptno);
? end loop;
end;
--11.定義游標:按工號從小到大的順序輸出雇員名字、工資以及工資與所在部門平均工資的差額:

declare
? cursor i_cur is?
? select e.ename,e.sal,e.sal-t.avg c from emp e,
? (select deptno,round(avg(sal),2) avg from emp group by deptno) t?
? where t.deptno = e.deptno order by e.sal;
begin
? for i in i_cur loop
? ? dbms_output.put_line(i.ename||' '
? ? ? ? ||i.sal||' '||i.c);
? end loop;
end;

--12.定義游標:以提升兩個資格最老的‘職員’(CLERK)為‘高級職員’(HIGHCLERK):(工作時間越長,優先級越高)

declare
? cursor j_cur is?
? select * from emp where job = 'CLERK' order by hiredate
? for update;
begin
? for j in j_cur loop
? ? if j_cur%rowcount < 3
? ? ? then
? ? ? ? update emp set job = 'HIGHCLERK' where current of j_cur;
? ? end if;
? end loop;
end;

select * from emp;
--13.使用顯式游標更新行,刪除薪資最低的那個員工:
declare
? cursor k_cur is select * from emp for update;
? min_sal number;
begin
? select min(sal) into min_sal from emp;
? for k in k_cur loop
? ? if k.sal = min_sal then
? ? ? ?delete from emp where current of k_cur;
? ? end if;
? end loop;
end;

----
declare
? cursor k_cur is select * from emp order by sal for update;
begin
? for k in k_cur loop
? ? if k_cur%rowcount = 1 then
? ? ? ?delete from emp where current of k_cur;
? ? end if;
? end loop;
end;

with soucre as (?
? ? select 1 as id , 3 as score from dual
? ? union all?
? ? select 2 as id , 4 as score from dual
? ? union all?
? ? select 3 as id , null as score from dual
? ? union all?
? ? select 4 as id , 3 as score from dual
? ? union all?
? ? select 5 as id , null as score from dual
? ? union all?
? ? select 6 as id , null as score from dual
? ? union all?
? ? select 7 as id , 5 as score from dual)?
select t.id,
nvl(t.score,lag(t.score)over(order by t.id)) score?
from (
select s.id,
nvl(s.score,lag(s.score)over(order by s.id)) score?
from soucre s)t
? ??
-- 測試數據表創建
with soucre as (?
? ? select 1 as id , 3 as score from dual
? ? union all?
? ? select 2 as id , 4 as score from dual
? ? union all?
? ? select 3 as id , null as score from dual
? ? union all?
? ? select 4 as id , 3 as score from dual
? ? union all?
? ? select 5 as id , null as score from dual
? ? union all?
? ? select 6 as id , null as score from dual
? ? union all?
? ? select 7 as id , 5 as score from dual) -- 測試數據表創建
select id,score,nvl(score,lag(score ignore nulls) over(order by id)) a from soucre;

/*create table customer(
cust_id number
,certificate_no char(18));
create table application(
apply_id number
,cust_id number
,amount number);
insert into customer values(1,370284199611045316);
insert into customer values(2,370284198011045316);
insert into customer values(3,370284196511045316);
insert into application values(11,1,700);
insert into application values(12,2,500);
insert into application values(13,3,200);*/
select * from customer;
select* from application;

select nvl(區間,'總計')區間,count(cust_id) 總人數,count(apply_id) 交易筆數,sum(amount)交易總金額 from(
select case when months_between(sysdate,d)/12 between 0 and 30 then '0-30歲'?
? ? ? ? ? ? when months_between(sysdate,d)/12 > 30 and months_between(sysdate,d)/12 <= 50 then '30-50歲'
? ? ? ? ? ? when months_between(sysdate,d)/12 > 50 then '50歲以上' end 區間,
cust_id,apply_id,amount from
(with t as
(select c.*,a.amount,a.apply_id from customer c,application a?
where a.cust_id = c.cust_id)
select cust_id,apply_id,to_date(substr(t.certificate_no,7,8),'yyyy-MM-dd') d,amount from t)
)group by rollup(區間);

------
with t as (select cust_id,apply_id,amount ,
? ? ? ? ? ? case when age between 0 and 30 then '0-30歲'?
? ? ? ? ? ? when age > 30 and age <= 50 then '30-50歲'
? ? ? ? ? ? when age > 50 then '50歲以上' end 區間
from (select c.cust_id,months_between(sysdate,to_date(substr(certificate_no,7,8),'yyyy-MM-dd'))/12 age,a.amount,a.apply_id?
from customer c,application a?
where a.cust_id = c.cust_id)?
)
select nvl(區間,'總計')區間,count(cust_id) 總人數,count(apply_id) 交易筆數,sum(amount)交易總金額 from t group by rollup(區間);
-----
with ca as
(select cust_id,amount,apply_id,
case when year between 0 and 30 then '0-30歲'?
? ? ?when year between 30 and 50 then '30-50歲'
? ? ?when year > 50 then '50歲以上'end age
from (select a.*,
to_char(sysdate,'yyyy')-substr(certificate_no,7,4) year
from customer c
join application a on c.cust_id=a.cust_id))
select nvl(age,'總計') 區間,count(cust_id) 總人數,count(*) 交易筆數,sum(amount) 交易總金額?
from ca group by rollup(age);


------------------------------------------------------------------------------------------------------------------

declare
? v_emp emp_bak%rowtype;
begin
? update emp_bak set comm=100 where deptno=&deptno;
? dbms_output.put_line('修改的數據條數:'||sql%rowcount);
? if sql%found then
? ? dbms_output.put_line('aaaaaaaaaaaaaaa');
? end if;
? delete from emp_bak where deptno=&dno;
? dbms_output.put_line('刪除了'||sql%rowcount||'條數據');?
end;

select * from emp_bak;
?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/42589.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/42589.shtml
英文地址,請注明出處:http://en.pswp.cn/web/42589.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

AFT:Attention Free Transformer論文筆記

原文鏈接 2105.14103 (arxiv.org) 原文翻譯 Abstract 我們介紹了 Attention Free Transformer (AFT)&#xff0c;這是 Transformer [1] 的有效變體&#xff0c;它消除了點積自注意力的需要。在 AFT 層&#xff0c;鍵key和值value首先與一組學習的位置偏差position biases相結…

ubuntu22安裝Docker并配置

安裝Docker sudo apt install docker.io使用腳本自動安裝docker&#xff1a; curl -fsSL get.docker.com -o get-docker.sh sudo sh get-docker.sh --mirror Aliyun配置國內鏡像 /etc/docker/daemon.json 推薦配置&#xff1a; {"registry-mirrors": ["htt…

Lab1 論文 MapReduce

目錄 &#x1f339;前言 &#x1f985;2 Programming Model &#x1f33c;2.1 Example &#x1f33c;2.2 Types &#x1f33c;2.3 More Examples &#x1f985;3 Implementation(實現) &#x1f33c;3.1 ~ 3.3 &#x1f33c;3.4 ~ 3.6 &#x1f985;4 Refinemen…

代理IP有什么用途

代理IP主要有以下應用場景&#xff1a; 1、隱藏真實IP地址&#xff1a;通過使用代理IP&#xff0c;可以隱藏真實的網絡請求來源&#xff0c;保護用戶隱私。 2、繞過網絡限制&#xff1a;一些地區或網絡環境可能存在訪問限制&#xff0c;通過使用代理IP可以繞過這些限制&#xf…

Anaconda+Pycharm 項目運行保姆級教程(附帶視頻)

最近很多小白在問如何用anacondapycharm運行一個深度學習項目&#xff0c;進行代碼復現呢&#xff1f;于是寫下這篇文章希望能淺淺起到一個指導作用。 附視頻講解地址&#xff1a;AnacondaPycharm項目運行實例_嗶哩嗶哩_bilibili 一、項目運行前的準備&#xff08;軟件安裝&…

BN的 作用

1、背景&#xff1a; 卷積神經網絡的出現&#xff0c;網絡參數量大大減低&#xff0c;使得幾十層的深層網絡成為可能。然而&#xff0c;在殘差網絡出現之前&#xff0c;網絡的加深使得網絡訓練變得非常不穩定&#xff0c;甚至出現網絡長時間不更新或者不收斂的情形&#xff0c;…

ER模型理論和三范式

ER模型理論和三范式 各種關系多對一一對一一對多多對多 三范式理論函數依賴完全函數依賴部分函數依賴傳遞&#xff08;間接&#xff09;函數依賴 第一范式&#xff1a;屬性&#xff08;表字段&#xff09;不可切割第二范式&#xff1a;不能存在 部分函數依賴(都存在完全函數依賴…

2款一鍵word生成ppt的AI工具,讓職場辦公更為簡單!

在當下主打異步溝通的職場辦公環境中&#xff0c;我們與很多人的溝通&#xff0c;都是通過書面材料來達成的&#xff0c;這就讓 Word 或文檔編輯軟件變得更為重要&#xff0c;與此同時&#xff0c;有時為了凸現書面材料中的重點&#xff0c;我們還要將 word 文檔轉換為 ppt 來進…

2024年06月CCF-GESP編程能力等級認證Python編程五級真題解析

本文收錄于專欄《Python等級認證CCF-GESP真題解析》&#xff0c;專欄總目錄&#xff1a;點這里&#xff0c;訂閱后可閱讀專欄內所有文章。 一、單選題&#xff08;每題 2 分&#xff0c;共 30 分&#xff09; 第 1 題 在Python中&#xff0c;print((c for c in “GESP”))的輸…

MiniGPT-Med 通用醫學視覺大模型:生成醫學報告 + 視覺問答 + 醫學疾病識別

MiniGPT-Med 通用醫學視覺大模型&#xff1a;生成醫學報告 視覺問答 醫學疾病識別 提出背景解法拆解 論文&#xff1a;https://arxiv.org/pdf/2407.04106 代碼&#xff1a;https://github.com/Vision-CAIR/MiniGPT-Med 提出背景 近年來&#xff0c;人工智能&#xff08;AI…

如何讓自動化測試框架更自動化?

一、引言 ?對于大廠的同學來說&#xff0c;接口自動化是個老生常談的話題了&#xff0c;畢竟每年的MTSC大會議題都已經能佐證了&#xff0c;不是大數據測試&#xff0c;就是AI測試等等&#xff08;越來越高大上了&#xff09;。不可否認這些專項的方向是質量智能化發展的方向&…

刷題(day02)

1、leetcode136.刪除鏈表的結點 給定單向鏈表的頭指針和一個要刪除的節點的值&#xff0c;定義一個函數刪除該節點。 返回刪除后的鏈表的頭節點。 示例 1: 輸入: head [4,5,1,9], val 5 輸出: [4,1,9] 解釋: 給定你鏈表中值為 5 的第二個節點&#xff0c;那么在調用了你的函數…

Windows圖形界面(GUI)-SDK-C/C++ - 應用程序結構

公開視頻 -> 鏈接點擊跳轉公開課程博客首頁 -> 鏈接點擊跳轉博客主頁 目錄 入口函數 窗口注冊 窗口創建 窗口顯示 窗口更新 消息循環 窗口過程 窗口銷毀 調試信息 示例代碼 入口函數 在Windows應用程序中&#xff0c;WinMain是主函數&#xff0c;作為應用程序…

網格化監控:Eureka與分布式服務網格的協同監控

網格化監控&#xff1a;Eureka與分布式服務網格的協同監控 引言 在微服務架構中&#xff0c;服務網格技術提供了一種有效的方式來管理和監控服務間的通信。Eureka作為Netflix開源的服務發現框架&#xff0c;雖然本身不直接提供服務網格的監控功能&#xff0c;但可以與服務網格…

設計模式探索:適配器模式

1. 適配器模式介紹 1.1 適配器模式介紹 適配器模式&#xff08;adapter pattern&#xff09;的原始定義是&#xff1a;將一個類的接口轉換為客戶期望的另一個接口&#xff0c;適配器可以讓不兼容的兩個類一起協同工作。 適配器模式的主要作用是把原本不兼容的接口&#xff0c…

【Python_GUI】thinker布局管理——place方法

place方法可以設置組件的大小以及組件在容器中的精確位置&#xff0c;其參數及含義如下&#xff1a; 參數含義X設置組件距離窗口左側的水平距離y設置組件距離窗口頂部的垂直距離width設置組件的寬度height設置組件的高度relx設置組件距離窗口左側的相對距離&#xff0c;范圍為…

c++初階學習----入門(上)

大家好啊。最近學習了一點關于c的知識。這不就迫不及待的來與大家分享了嘛。但我這也是現學現賣所以咧。有很多遺落甚至不對的地方希望大家可以在評論區里面指出來。這樣也可以增加大家對知識的鞏固。 c語言與c的聯系 不知道大家看到c會不會不由自主的聯想到C語言啊。畢竟都是…

手機自帶錄屏在哪?6個軟件教你快速進行手機錄屏

手機自帶錄屏在哪&#xff1f;6個軟件教你快速進行手機錄屏 手機自帶的錄屏功能可以讓你輕松錄制屏幕上的內容&#xff0c;記錄游戲過程、制作教程或捕捉其他重要時刻。不同品牌的手機可能在不同位置提供錄屏功能。以下是一些常見的手機品牌及其錄屏功能位置&#xff0c;以及一…

【康復學習--LeetCode每日一題】724. 尋找數組的中心下標

題目&#xff1a; 給你一個整數數組 nums &#xff0c;請計算數組的 中心下標 。 數組 中心下標 是數組的一個下標&#xff0c;其左側所有元素相加的和等于右側所有元素相加的和。 如果中心下標位于數組最左端&#xff0c;那么左側數之和視為 0 &#xff0c;因為在下標的左側不…

運動愛好者的新選擇:哈氪聆光氣傳導耳機,輕巧又安全

平時不管是漫步街頭、騎行穿梭&#xff0c;還是乘坐公共交通時&#xff0c;我總是喜歡佩戴耳機&#xff0c;借此隔絕外部的喧囂&#xff0c;享受音樂的樂趣。在戶外使用耳機&#xff0c;我更傾向于選擇氣傳導耳機&#xff0c;它們更符合我的需求&#xff0c;因為這種耳機能讓我…