PLSQL

n 在客户端输出helloworld

set serveroutput on;//默认是off,设成on是让Oracle可以在客户端输出数据    

begin    

    dbms_output.put_line('helloworld');    

end;    

  / 

n 变量的赋值与输出

declare    

        v_name varchar2(20);//声明变量v_name变量的声明以v_开头    

    begin    

        v_name := 'myname';    

        dbms_output.put_line(v_name);    

    end;    

    /    

n pl/sql对于异常的处理(除数为0)

declare    

        v_num number := 0;    

    begin    

        v_num := 2/v_num;    

        dbms_output.put_line(v_num);    

    exception    

        when others then    

        dbms_output.put_line('error');    

    end;    

    /  

n 基本变量类型

binary_integer:整数,主要用来计数而不是用来表示字段类型。比number效率高    

    number:数字类型    

    char:定长字符串    

    varchar2:变长字符串    

    date:日期    

long:字符串,最长2GB    

boolean:布尔类型,可以取值true,false,null//最好给一初值  

变量的声明,使用 '%type'属性

declare    

        v_empno number(4);    

        v_empno2 emp.empno%type; // v_empno2的类型和emp表的empno字段的类型一样

        v_empno3 v_empno2%type;  // v_empno3的类型和变量v_empno2的类型一样 

    begin    

        dbms_output.put_line('Test');    

    end;    

    /    

//使用%type属性,可以使变量的声明根据表字段的类型自动变换,省去了维护的麻烦,而且%type属性,可以用于变量身上

n table变量类型(数组)

declare

   //定义一种数组类型,内容的类型是emp.empno%type,下标类型为binary_integer

   type type_table_emp_empno is table of emp.empno%type index by binary_integer;

   //声明一个数组变量

   v_empnos type_table_emp_empno;

begin

   v_empnos(0) := 7369;

    v_empnos(2) := 7839;

    v_empnos(-1) := 9999;

    dbms_output.put_line(v_empnos(-1));

end;

n record变量类型(结构体)

declare

  type type_record_dept is record

      (

        deptno dept.deptno%type,

        dname dept.dname%type,

        loc dept.loc%type

      );

    v_temp type_record_dept;

begin

  v_temp.deptno := 50;

  v_temp.dname := 'aaa';

  v_temp.loc := 'bj';

  dbms_output.put_line(v_temp.deptno || ' ' || v_temp.dname);

end;

n 使用%rowtype声明record变量

declare

  v_temp dept%rowtype;//结构体里的变量就是dept表的字段

begin

    v_temp.deptno := 50;

    v_temp.dname := 'aaa';

    v_temp.loc := 'bj';

   dbms_output.put_line(v_temp.deptno || ' ' || v_temp.dname);

end;

n select语句

必须且只能返回一条记录

declare

     v_name emp.ename%type;

     v_sal emp.sal%type;

begin

   select ename, sal into v_name, v_sal from emp where empno = 7369;

   dbms_output.put_line(v_name || ' ' || v_sal);

end;

declare

    v_emp emp%rowtype;

begin

  select * into v_emp from emp where empno = 7369;

  dbms_output.put_line(v_emp.ename);

end;

n insert语句

declare

    v_deptno dept.deptno%type := 50;

    v_dname dept.dname%type := 'aaa';

    v_loc dept.loc%type := 'bj';

begin

  insert into dept2 values(v_deptno, v_dname, v_loc);

 commit;

end;

n update语句

declare

   v_deptno emp2.deptno%type := 10;

   v_count number;

begin

  --update emp2 set sal = sal/2 where deptno = v_deptno;

  --select deptno into v_deptno from emp2 where empno = 7369;

  select count(*) into v_count from emp2;

  //sql是一个关键字,rowcount是sql的一个属性。

  dbms_output.put_line(sql%rowcount || '条记录被影响');

 commit;

end;

n DDL语言,数据定义语言 

begin

     execute immediate 'create table t (nnn varchar2(20) default ''aaa'')';

end;

n if语句

取出7369的薪水,如果<1200,输出'low',如果<2000输出'middle',否则'high'

declare

    v_sal emp.sal%type;

begin

   select sal into v_sal from emp

          where empno = 7369;

   if (v_sal < 1200) then

         dbms_output.put_line('low');

   elsif (v_sal < 2000) then

         dbms_output.put_line('middle');

   else

         dbms_output.put_line('high');

   end if;

end;

n 循环语句

declare

   i binary_integer := 1;

begin

   loop

      dbms_output.put_line(i);

      i := i + 1;

      exit when (i >= 11);

   end loop;

end;

---------

declare

   j binary_integer := 1;

begin

  while j < 11 loop

      dbms_output.put_line(j);

          j := j + 1;

  end loop;

end;

-----------

begin

    for k in 1..10 loop

       dbms_output.put_line(k);

    end loop;

    for k in reverse 1..10 loop

        dbms_output.put_line(k);

     end loop;

end;

n 错误处理

declare

   v_temp number(4);

begin

   select empno into v_temp from emp where empno = 10;

exception

   when too_many_rows then

      dbms_output.put_line('太多纪录了');

   when others then

      dbms_output.put_line('error');

end;

----------

declare

   v_temp number(4);

begin

   select empno into v_temp from emp where empno = 2222;

exception

   when no_data_found then

      dbms_output.put_line('没有数据');

end;

n 日志程序

    --创建事件日志表

create table errorlog

(

id number primary key,

errcode number,

errmsg varchar2(1024),

errdate date

)

--创建序列

create sequence seq_errorlog_id start with 1 increment by 1 

--实验

declare

   v_deptno dept.deptno%type := 10;

   v_errcode number;

   v_errmsg varchar2(1024);

begin

   delete from dept where deptno = v_deptno;

 commit;

exception

   when others then

      rollback;

         v_errcode := SQLCODE;

         v_errmsg := SQLERRM;

      insert into errorlog values (seq_errorlog_id.nextval, v_errcode, v_errmsg, sysdate);

      commit;

end;

n 游标

declare

    cursor c is

       select * from emp;

    v_emp c%rowtype;

begin

    open c;

    loop

      fetch c into v_emp;

      exit when (c%notfound);

      dbms_output.put_line(v_emp.ename);

    end loop;

    close c;

end;

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

declare

    cursor c is

       select * from emp;

    v_emp c%rowtype;

begin

    open c;

    fetch c into v_emp;

    while (c%found) loop

      dbms_output.put_line(v_emp.ename);

      fetch c into v_emp;

    end loop;

    close c;

end;

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

declare

    cursor c is

       select * from emp;

begin

   for v_emp in c 

loop

        dbms_output.put_line(v_emp.ename);

    end loop;

    end;

n 带参数的游标

declare

   cursor c (v_deptno emp.deptno%type, v_job emp.job%type)//声明形参

   is

     select ename, sal from emp where deptno = v_deptno and job = v_job;

begin

   for v_temp in c(30,'CLERK') //v_temp不需要声明

loop

      dbms_output.put_line(v_temp.ename);

    end loop;

end;

n 可更新的游标

declare

  cursor c

  is

    select * from emp2 for update;

begin

   for v_temp in c loop

      if (v_temp.sal < 2000) then

         update emp2 set sal = sal * 2 where current of c;

      elsif (v_temp.sal = 5000) then

         delete from emp2 where current of c;

      end if;

    end loop;

    commit;

end;

n 存储过程

存储过程出现编译错误,用show errors命令查看出错信息。

create or replace procedure p

is

  cursor c

  is

    select * from emp2 for update;

begin

   for v_temp in c loop

      if (v_temp.deptno = 10) then

         update emp2 set sal = sal + 10 where current of c;

      elsif (v_temp.deptno = 20) then

         update emp2 set sal = sal + 20 where current of c;

      else

         update emp2 set sal = sal + 50 where current of c;

      end if;

    end loop;

    commit;

end;

--执行 

exec p;

begin;

 p;

end;

n 带参数的存储过程

//in代表该参数为传入参数,out代表该参数为传出参数。不写默认为in。

create or replace procedure p

     (v_a in number, v_b number, v_ret out number, v_temp in out number)

is

begin

   if (v_a > v_b) then

      v_ret := v_a;

   else

      v_ret := v_b;

   end if;

   v_temp := v_temp + 1;

end;

--实验

declare

 v_a number := 3;

 v_b number := 4;

 v_ret number;

 v_temp number := 5;

begin

 p(v_a, v_b, v_ret, v_temp);

 dbms_output.put_line(v_ret);

 dbms_output.put_line(v_temp);

end;

n 函数

create or replace function sal_tax(v_sal number) return number

is

begin

   if (v_sal < 2000) then

      return 0.10;

   elsif (v_sal < 2750) then

      return 0.15;

   else

      return 0.20;

   end if;

    end;

n 触发器

create table emp2_log

(

uname varchar2(20),

action varchar(10),

atime date

)

-----------

create or replace trigger trig

  after insert or update or delete on emp2//触发条件

begin

  if inserting then

     insert into emp2_log values (USER, 'insert', sysdate);

  elsif updating then

     insert into emp2_log values (USER, 'update', sysdate);

  elsif deleting then

     insert into emp2_log values (USER, 'delete', sysdate);

  end if;

end;

-----------

create or replace trigger trig

  after insert or update or delete on emp2 for each row//每更新一行就会触发一次

begin

  if inserting then

     insert into emp2_log values (USER, 'insert', sysdate);

  elsif updating then

     insert into emp2_log values (USER, 'update', sysdate);

  elsif deleting then

     insert into emp2_log values (USER, 'delete', sysdate);

  end if;

end;

-----------

用这种方法可以修改外键约束的字段。

create or replace trigger trig

  after update on dept for each row

begin

//NEW代表操作后的记录,OLD代表操作前的记录

 update emp2 set deptno = :NEW.deptno where deptno = :OLD.deptno;

end;

n 树状结构的存储和展示

create table article

(

id number primary key,

cont varchar2(4000),

pid number,--父id

isleaf number(1), --0代表非叶子节点,1代表叶子节点

alevel number(2)

)

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

insert into article values (1, '蚂蚁大战大象', 0, 0, 0);

insert into article values (2, '大象被打趴下了', 1, 0, 1);

insert into article values (3, '蚂蚁也不好过', 2, 1, 2);

insert into article values (4, '瞎说', 2, 0, 2);

insert into article values (5, '没有瞎说', 4, 1, 3);

insert into article values (6, '怎么可能', 1, 0, 1);

insert into article values (7, '怎么没可能', 6, 1, 2);

insert into article values (8, '可能性是很大的', 6, 1, 2);

insert into article values (9, '大象进医院了', 2, 0, 2);

insert into article values (10, '护士是蚂蚁', 9, 1, 3);

commit;

---------

蚂蚁大战大象

   大象被打趴下了

      蚂蚁也不好过

      瞎说

         没有瞎说

      大象进医院了

         护士是蚂蚁

   怎么可能

         怎么不可能

         可能性是很大的

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

create or replace procedure p (v_pid article.pid%type, v_level binary_integer) is

  cursor c is select * from article where pid = v_pid;

  v_preStr varchar2(1024) := '';

begin

  for i in 1..v_level loop

    v_preStr := v_preStr || '****';

  end loop;

  for v_article in c loop

    dbms_output.put_line(v_preStr || v_article.cont);

  if (v_article.isleaf = 0)

then

    p (v_article.id, v_level + 1);

  end if;

  end loop;

end;

资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持多种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问题。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个项目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值