2018-1-9

1、使用for循环来提取使用游标,for循环可以自动的打开游标,每一次循环都可以自动的提取一次游标,循环结束后会自动关闭游标

   格式:

         for 变量名 in 游标名 loop

             使用游标语句

         end loop

   例:1declare 

           cursor cur is

           select t.tno,t.tname,t.tage

           from teacher t;

begin 

           for c in cur loop

           dbms_output.put_line(c.tno ||','||c.tname ||','||c.tage );

           end loop;

end;

2/*利用游标,调整员工工资:

   0——8500     5%(不含8500

   8500——9000  3%(不含9000

   9000——      1%*/

declare

   cursor cur is 

   select t.tno,t.tname,t.sal

   from teacher t;

   v_temp number(8,2);

begin

  for c in cur loop

    if c.sal<8500

    then v_temp:=0.05;

    elsif c.sal<9000

    then v_temp:=0.03;

    else v_temp:=0.01;

    end if;

    update teacher set sal=sal*(1+v_temp)

    where tno=c.tno;

  end loop;

end;

2、异常处理

   Oracle为了在程序中解决异常的问题,提供了exception代码块,用来解决程序运行发生的异常

1、预定义的异常处理:oracle提供了一些常见的错误,并为它们起好了名字。

格式:exception

      when  错误名 then 处理语句

例:declare 

       v_tno teacher.tno%type;

begin

       select tno into v_tno from teacher;

exception

        when too_many_rows then

          dbms_output.put_line('对不起,查询的结果过多');

end;

2、非预定义的异常处理:oracle没有指定名字的错误信息,需要我们自己来指定,在声明部分定义变量,并且为该变量关联错误编号。

   格式:声明部分定义变量:变量名 exception

                           pragma exception_init(变量名,错误编号)

                           使用方法和预定的异常相同

   例:declare

          deletebyid exception;

          pragma exception_init(deletebyid,-02292);

begin

           delete from dept d where d.dno='5';

exception

           when deletebyid then

           dbms_output.put_line('对不起,您所要删除的系别正在被使用,无法删除');

end;

3、自定义异常处理:在程序的执行体中,可以使用  raise 异常变量名  来将定义的异常抛出。抛出后,就可以在exception代码块中像处理预定义异常一样的写法去处理。

    例:declare

            e_sal_toohigh exception;

            v_sal tempteacher.sal%type;

begin

            select sal into v_sal from tempteacher where tno=20;

            if v_sal > 30000 then

            raise e_sal_toohigh;    /*将定义的异常抛出*/

            else

            dbms_output.put_line(v_sal);

            end if;

exception

            when e_sal_toohigh then

            dbms_output.put_line('对不起,该员工工资异常');

end;

3、函数

   函数可以将pl/sql代码保存在数据库中,供其他程序调用

   格式:create or replace function 函数名(参数)

         return 返回值类型

         is

            定义变量名

         begin

            程序体

         end;

    例:1create or replace function goodbye

return varchar2

is

begin

           return='再见';

end;

 

1select goodbye from dual

 

2begin                                        函数的调用

             dbms_output.put_line(goodbye);

end;

     2/*编写一个函数,返回当前系统时间,并调用该函数*/

create or replace function clock

return date

is

v_clock date;

begin

  select sysdate into v_clock from dual;

  return v_clock;

end;

  

1select clock from dual;

 

2begin                                              调用函数

     dbms_output.put_line(clock);

end;

 

3/*编写一个函数,传入字符串xxx,返回 helloxxx*/  

create or replace function hellostr(str varchar2)    定义可传入字符串函数

return  varchar2

is

begin

  return 'hello,'||str;

end;

  

begin

  dbms_output.put_line(hellostr('Tom'));

end;                                                调用函数

 

select hellostr('Tom') from dual;

4/*输入两个数,求和*/  

create or replace function sumfun(a number,b number)

return number

is

s number;

begin

  s:=a+b;

  return s;

end;

 

select sumfun(6,3) from dual

5/*输入一个系别编号,得到该系别所有老师的工资总和*/

create or replace function sumsal(v_dno teacher.dno%type)

return teacher.sal%type

is

v_sum teacher.sal%type;

begin

  select sum(t.sal) into v_sum from teacher t where t.dno = v_dno;

  return v_sum;

end;

4、存储过程

   存储过程也可以将PL/SQL保存到数据库中,供其他程序调用。

   格式:

       create or replace procedure 过程名(参数)

       is

       定义变量

       begin

       程序体

       end;

注意:存储过程和函数的差别在于存储过程没有返回值,而函数则有返回值

例:1create or replace procedure 

insdept(v_dno dept.dno%type,v_dname dept.dname%type,v_stucount dept.stucount%type)

is

begin

  insert into dept values (v_dno,v_dname,v_stucount);

  dbms_output.put_line('数据插入成功');

end;

     2create or replace procedure deldept(dno dept.dno%type)

is

  e_del exception;

  v_count number;

begin

  select count(*)into v_count from dept d where d.dno=v_dno;

  if v_count >0

  then delete from dept where dno=v_dno;

  dbms_output.put_line('删除成功');

  else

    raise e_del;

  end if;

exception

    when e_del then

      dbms_output.put_line('对不起,传入的编号不存在!') ;

end;

5、删除函数和存储过程

   删除函数——drop function 函数名

   删除存储过程——drop procedure  存储过程名

6、触发器

   定义:触发器是在根据指定表中记录被新增,修改或者删除时所触发执行的PL/SQL代码。它可以设置为beforeafter。同时还可以设置为行级或语句级(for each row表示行级)

那么对于每一张表,可以创建12种触发器

格式:

create or replace trigger 触发器名

before after

insert update delete

[for each row]

begin

    执行代码

end;

例:create or replace trigger hellotrigger

        after update on tempteacher

        for each row                                 

begin

        dbms_output.put_line('helloworld!');

end;

注意:在触发器中可以使用:old来表示操作之前的数据;使用new来表示操作之后的数据

例:create table my_teacher as select t.tno,t.tname,t.tage from teacher t where 1=2;

create table my_teacher_bak as select t.tno,t.tname,t.tage from teacher t where 1=2;

create or replace trigger bak_my_teacher

  after delete on my_teacher

  for each row

begin

  insert into my_teacher_bak values(:old.tno,:old.tname,:old.tage);

end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码将给定的列表转换成一个 Pandas 数据框(DataFrame): ```python import pandas as pd # 给定的列表数据 data = [['日期', '2018-01-03', -1.172468, '2018-01-04', -0.258565, '2018-01-05', 0.163079, '2018-01-08', -1.179654, '2018-01-09', 0.411397, '2022-12-26', -0.784676, '2022-12-27', 1.262722, '2022-12-28', 0.104694, '2022-12-29', -0.408606, '2022-12-30', 0.484443], ['日期', '2018-01-03', -0.329916, '2018-01-04', -0.012197, '2018-01-05', -0.615016, '2018-01-08', -1.149181, '2018-01-09', -0.173742, '2022-12-26', -0.177332, '2022-12-27', 1.786322, '2022-12-28', 1.108457, '2022-12-29', -0.693480, '2022-12-30', 0.333768], ['日期', '2018-01-03', -0.295204]] # 将列表转换成数据框 df = pd.DataFrame(data) # 设置列名 df.columns = ['col' + str(i) for i in range(df.shape[1])] # 将第一列作为行索引 df = df.set_index('col0') # 将奇数列作为日期列,偶数列作为数值列 df_dates = df.iloc[:, ::2] df_values = df.iloc[:, 1::2] df_dates.columns = range(df_dates.shape[1]) df_values.columns = range(df_values.shape[1]) # 将日期列和数值列合并成一个数据框 df = pd.concat([df_dates.stack(), df_values.stack()], axis=1) df.columns = ['日期', '数值'] # 将日期列转换成日期类型 df['日期'] = pd.to_datetime(df['日期']) ``` 注意:给定的列表中有多个子列表,每个子列表包含了日期和数值数据。我们需要将这些子列表转换成一个 Pandas 数据框,并将日期列和数值列拆分成两个数据框,然后再将它们合并成一个数据框。最后,我们还需要将日期列转换成 Pandas 中的日期类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值