PLSQL Day6

news/2024/7/24 3:43:41 标签: 数据结构, sql, oracle

declare
  type i_type is table of varchar2(50) index by varchar2(50);
  tab i_type;
  idx varchar2(50);
begin
  tab('A'):='东邪';
  tab('a'):='西毒';
  tab('g'):='南帝';
  tab('d'):='北丐';
  tab('p'):='中神通';
  idx := tab.first;
  loop
    dbms_output.put_line(tab(idx));
    exit when idx=tab.last;
    idx:=tab.next(idx);
  end loop;
end;

----------------------------------------------------------------------------
declare
  type i_tab is table of varchar2(50) 
index by varchar2(50);
  tab i_tab;
  i varchar2(50);
begin
  tab('a'):='东邪';
  tab('b'):='西毒';
  tab('c'):='南帝';
  tab('d'):='北丐';
  tab('e'):='中神通';
  i:=tab.first;
  <<A>>
  dbms_output.put_line(tab(i));
  i:=tab.next(i);
  if i is not null then
    goto A;
  end if;
end;

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

declare
   --声名一个索引表类型
   type mytype is table of varchar2(200) index by varchar2(5);
   --声名一个索引表类型变量
   tab mytype;
   --声名一个变量保存索引表的下标
   ind varchar2(5);
begin
  tab('d'):='东邪';
  tab('e'):='西毒';
  tab('b'):='南帝';
  tab('a'):='北丐';
  tab('f'):='中神通';
  ind:=tab.first;
   ---遍历集合
   while ascii(ind)<=ascii(tab.last) loop
      --打印集合元素
      dbms_output.put_line(tab(ind));
      ind:=tab.next(ind);--ind:=ind+1;
   end loop;
end;

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

--4.取出所有员工的薪资(sal),存入嵌套表中,通过遍历集合得出总工资和:
declare
   type a_type is table of emp%rowtype ;
   e_tab a_type := a_type();
   x number := 1;
   all_sal number := 0;
begin
  for i in (select * from emp) loop
    e_tab.extend;
    e_tab(x) := i;
    x := x+1;
  end loop;
  for j in 1..e_tab.count loop
    all_sal := all_sal + e_tab(j).sal;
  end loop;
  dbms_output.put_line('总工资为:'||all_sal);
end;

--5.创建一个与emp字段相同的空表copy_emp,
--利用集合把emp表中的数据导入copy_emp:

-- create or replace type copy_type is table of emp_type;
create table copy_emp
as (select * from emp where 1=0);

declare
  type b_type is table of emp%rowtype;
  e_tab b_type := b_type();
  x number := 1;
begin
  for i in (select * from emp) loop
    e_tab.extend;
    e_tab(x) := i;
    x := x+1;
  end loop;
  for j in 1..e_tab.count loop
    insert into copy_emp values(e_tab(j).empno,e_tab(j).ename,
    e_tab(j).job,e_tab(j).mgr,e_tab(j).hiredate,e_tab(j).sal,
    e_tab(j).comm,e_tab(j).deptno);
  end loop;
end;


select * from copy_emp;


--6.按部门编号将员工存入不同的集合,再取出打印:
declare
  type c_type is table of emp%rowtype;
  e_tab c_type := c_type();
  x number := 1;
  dno number := &部门号;
begin
  for i in (select * from emp where deptno = dno) loop
    e_tab.extend;
    e_tab(x) := i;
    x := x+1;
  end loop;
  for j in 1..e_tab.count loop
    dbms_output.put_line(e_tab(j).empno||e_tab(j).ename||'  '||
    e_tab(j).job||'  '||e_tab(j).mgr||'  '||e_tab(j).hiredate
    ||'  '||e_tab(j).sal||'  '||
    e_tab(j).comm||'  '||e_tab(j).deptno);
  end loop;
end;

--
declare
  type d_type is table of emp%rowtype;
  e_10_tab d_type := d_type();
  e_20_tab d_type := d_type();
  e_30_tab d_type := d_type();
  x number := 1;
  y number := 1;
  z number := 1;
begin
  for i in (select * from emp where deptno = 10) loop
    e_10_tab.extend;
    e_10_tab(x):=i;
    x:=x+1;
  end loop;
  for i in (select * from emp where deptno = 20) loop
    e_20_tab.extend;
    e_20_tab(y):=i;
    y:=y+1;
  end loop;
  for i in (select * from emp where deptno = 30) loop
    e_30_tab.extend;
    e_30_tab(z):=i;
    z:=z+1;
  end loop;
  for j in 1..e_10_tab.count loop
    dbms_output.put_line(e_10_tab(j).empno||e_10_tab(j).ename||'  '||
    e_10_tab(j).job||'  '||e_10_tab(j).mgr||'  '||e_10_tab(j).hiredate
    ||'  '||e_10_tab(j).sal||'  '||e_10_tab(j).comm||'  '||e_10_tab(j).deptno);
  end loop;
  for j in 1..e_20_tab.count loop
    dbms_output.put_line(e_20_tab(j).empno||e_20_tab(j).ename||'  '||
    e_20_tab(j).job||'  '||e_20_tab(j).mgr||'  '||e_20_tab(j).hiredate
    ||'  '||e_20_tab(j).sal||'  '||e_20_tab(j).comm||'  '||e_20_tab(j).deptno);
  end loop;
  for j in 1..e_30_tab.count loop
    dbms_output.put_line(e_30_tab(j).empno||e_30_tab(j).ename||'  '||
    e_30_tab(j).job||'  '||e_30_tab(j).mgr||'  '||e_30_tab(j).hiredate
    ||'  '||e_30_tab(j).sal||'  '||    e_30_tab(j).comm||'  '||e_30_tab(j).deptno);
  end loop;
end;

--7.将所有员工信息保存到一个集合中,然后删除下标为3,5,7,及最后三个下标对应的元素。
-- 然后给下标为5的元素重新赋值,最后输出集合中元素的个数:
declare
  type e_type is table of emp%rowtype;
  e_tab e_type := e_type();
  x number := 1;
  cnt number := 0;
begin
  for i in (select * from emp) loop
    e_tab.extend;
    e_tab(x) := i;
    x := x+1;
  end loop;
  cnt := e_tab.count;
  for j in 1..e_tab.count loop
    if j in (3,5,7,cnt,cnt-1,cnt-2) then
      e_tab.delete(j);
    end if;
  end loop;
  e_tab(5) := null; 
  dbms_output.put_line(e_tab.count);
end;
select * from emp;

--8.编写一个点名器,声明一个集合,存储五个同学的名字,
-- 每次执行后会随机输出一个名字,并且输出后,
--该名字在下次执行时不会出现(生成1-5随机整数:trunc(dbms_random.value(1,6))):

create table sttuu(sname varchar2(50));
insert into sttuu values('东邪');
insert into sttuu values('西毒');
insert into sttuu values('南帝');
insert into sttuu values('北丐');
insert into sttuu values('中神通');

truncate table sttuu;
select * from sttuu;
declare
  type stu_type is table of sttuu%rowtype;
  s_tab stu_type := stu_type();
  cursor cur is select * from sttuu;
  x number := 1;
  n number;
  i number := 5;
begin
  select count(*) into i from sttuu;
  s_tab.extend(i);
  for s_cur in cur loop
    s_tab(x).sname := s_cur.sname;
    x := x+1;
  end loop;
  if i > 0 then
  n := trunc(dbms_random.value(1,i));
  dbms_output.put_line(s_tab(n).sname); 
  delete from sttuu where sname = s_tab(n).sname;     
  s_tab.delete(n); 
  else  
    dbms_output.put_line('NO DATA!');
  end if;
end;


----
declare
   type stu_type is record(
     sname varchar(50)    
   );
   type s_type is table of stu_type;
   stu s_type := s_type();
   nid number ;
   x number;
begin
  stu.extend(5);
  stu(1).sname := '东邪'; 
  stu(2).sname := '西毒';
  stu(3).sname := '南帝';
  stu(4).sname := '北丐';
  stu(5).sname := '中神通';
  x := stu.count;
  <<A>>
  while x>0 loop
    nid := trunc(dbms_random.value(1,6)); 
    if stu.exists(nid) then
      dbms_output.put_line(stu(nid).sname);
      stu.delete(nid); 
      x:=x-1;
    end if;
    goto A;
  end loop;
end;


create table sttuu(sname varchar2(50));
insert into sttuu values('东邪');
insert into sttuu values('西毒');
insert into sttuu values('南帝');
insert into sttuu values('北丐');
insert into sttuu values('中神通');

truncate table sttuu;
select * from sttuu;
declare
  type stu_type is table of sttuu%rowtype;
  s_tab stu_type := stu_type();
  cursor cur is select * from sttuu;
  x number := 1;
  n number;
  i number := 5;
begin
  select count(*) into i from sttuu;
  s_tab.extend(i);
  for s_cur in cur loop
    s_tab(x).sname := s_cur.sname;
    x := x+1;
  end loop;
  if i > 1 then
  n := trunc(dbms_random.value(1,i));
  dbms_output.put_line(s_tab(n).sname); 
  delete from sttuu where sname = s_tab(n).sname;     
  s_tab.delete(n); 
  else 
    dbms_output.put_line(s_tab(1).sname); 
    delete from sttuu where sname = s_tab(1).sname;     
    s_tab.delete(1); 
  end if;
end;


 


http://www.niftyadmin.cn/n/5546948.html

相关文章

泛微E9开发 控制日期浏览按钮的可选日期范围

控制日期浏览按钮的可选日期范围 1、需求说明2、实现方法3、扩展知识点控制日期浏览按钮的可选日期范围格式参数说明演示 1、需求说明 控制日期浏览按钮的可选日期范围为2024/07/01~2024/07/31&#xff0c;如下图所示 2. 控制日期浏览按钮的可选日期范围在当前时间的前一周~当…

如何在 PostgreSQL 中确保数据的异地备份安全性?

文章目录 一、备份策略1. 全量备份与增量备份相结合2. 定义合理的备份周期3. 选择合适的备份时间 二、加密备份数据1. 使用 PostgreSQL 的内置加密功能2. 使用第三方加密工具 三、安全的传输方式1. SSH 隧道2. SFTP3. VPN 连接 四、异地存储的安全性1. 云存储服务2. 内部存储设…

SQL的时间格式和文本灵活转换

日期的格式&#xff0c;在日常的数据分析中&#xff0c;常常使用 特别是在按照日、月、年进行汇总分析&#xff0c;使用起来&#xff0c;往往会有差异 如果格式比较复杂&#xff0c;可以考虑进行文本转化的处理 这里有比较推荐的函数&#xff1a; 1.CONVERT()函数 适用于SQL …

安全封装:Conda包签名验证的终极指南

安全封装&#xff1a;Conda包签名验证的终极指南 引言 软件包的安全性是科学研究和生产环境中不可忽视的一环。Conda作为流行的Python包管理器&#xff0c;提供了包签名验证功能&#xff0c;以确保包的完整性和来源可靠性。本文将深入探讨如何在Conda中使用包签名验证&#x…

【自学网络安全】:安全策略与用户认证综合实验

实验拓扑图&#xff1a; 实验任务&#xff1a; 1、DMZ区内的服务器&#xff0c;办公区仅能在办公时间内(9:00-18:00)可以访问&#xff0c;生产区的设备全天可以访问 2、生产区不允许访问互联网&#xff0c;办公区和游客区允许访问互联网 3、办公区设备10.0.2.10不允许访问Dmz区…

守护服务之门:Eureka中分布式认证与授权的实现策略

守护服务之门&#xff1a;Eureka中分布式认证与授权的实现策略 引言 在微服务架构中&#xff0c;服务间的通信安全至关重要。Eureka作为Netflix开源的服务发现框架&#xff0c;虽然本身提供了服务注册与发现的功能&#xff0c;但并不直接提供认证与授权机制。为了实现服务的分…

7月8日 四道经典单链表oj题

大家好呀&#xff0c;本博客目的在于记录暑假学习打卡&#xff0c;后续会整理成一个专栏&#xff0c;主要打算在暑假学习完数据结构&#xff0c;因此会发一些相关的数据结构实现的博客和一些刷的题&#xff0c;个人学习使用&#xff0c;也希望大家多多支持&#xff0c;有不足之…

算法学习记录3

L1-077 大笨钟的心情 mood_levels list(map(int, input().split()))# 存储询问时间点 lines []# 获取询问时间点&#xff0c;直到遇到非法输入 while True:ask_time input()if ask_time "":breaklines.append(int(ask_time))# 遍历询问时间点并判断对应心情指数 …