oracle常见sql语句

本文详细介绍了在SQL中如何使用存储过程进行数据的增删改查操作,并演示了通过集合函数遍历输出数据的过程。从创建存储过程到具体应用案例,再到集合函数的灵活运用,文章全面覆盖了数据库操作的关键技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

存储过程
1.向表中添加数据的存储过程

create or replace procedure gxs_add_2(g_id in number,g_name in varchar,g_sex in varchar,g_address in varchar) as
begin
  insert into gxs_stu_info (id,name,sex,address) values(g_id,g_name,g_sex,g_address);
end gxs_add_2;

2.查询的存储过程

create or replace procedure gxs_select_by_name(g_name in varchar,g_sex out varchar)as
begin
  select sex into g_sex from gxs_stu_info where name = g_name;
end;

3.修改的存储过程

create or replace procedure gxs_update(g_id in number,g_name in varchar,g_sex in varchar,g_address in varchar) as
begin
  update gxs_stu_info set name = g_name, sex=g_sex ,address=g_address where id = g_id;
end;

4.删除的存储过程

create or replace procedure gxs_delete(g_id in number,g_name in varchar) as
begin
  delete from gxs_stu_info where id = g_id or name = g_name;
end;

集合函数
遍历输出

--创建集合并遍历输出
declare
type list_country is table of varchar2(100) not null;
list_all list_country := list_country('中国','美国','德国','英国','西班牙','澳大利亚');
begin
  for x in list_all.first .. list_all.last loop
    dbms_output.put_line(list_all(x));
    end loop;
end;

删除

declare
type list_people is table of varchar2(100) not null;
list_all list_people := list_people('皮皮 ','芳芳','菲菲','红红','健健','亮亮');
begin
  --删除指定位置的数据
  list_all.delete(1);
  --删除指定范围的数据
  list_all.delete(1,4);
  for x in list_all.first .. list_all.last loop
    dbms_output.put_line(x||'---'||list_all(x));
  end loop;
end;

根据索引判断相应数据是否存在

declare
type list_foods is table of varchar2(100) not null;
list_all list_foods :=list_foods('苹果','香蕉','梨子','菠萝','冬瓜','西瓜','南瓜');
begin
  --list_all.delete(1);
  if list_all.exists(1) then
    dbms_output.put_line('索引为1的数据存在');
    end if;
    if not list_all.exists(10) then
      dbms_output.put_line('索引为10的数据不存在');
    end if;
end;
/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值