2020-08-24oracle学习

2020-08-25  本文已影响0人  ElfACCC

2020-08-24

1、string用varchar2,date 用Date,数字用number(总长度,小数位数)
2、去重:select distinct name from table;
3、用||连接字符串
4、别名不用加单引号

2020-08-25

2020-08-26

declare
  (常量和变量的定义)
  pname varchar2(15);--pname 是变量名,一定要有分号;
  pasl number(9,2);
  age number(3) :=  20;  --也可以直接赋值
  emprec emp.ename%type --引用赋值,把emp表中的ename的类型赋给emprec
  prec  myemp%rowtype;   --记录型变量,对应java中对象类型的变量
begin
  select *  into prec from myemp t where t.empo = 0999;
  dbms_output.put_line(prec.ename || '   ' ||prec.sal);  --记录型变量可以直接点出来属性值
  select t.ename into emprec  from emp t where t.empno = 7777; --引用赋值,into的用法
  panme :=  'zangsna'     --   :=是赋值
  dbms_output.put_line('hello world'); -- 输出打印函数
exception

end;

declare
  pno number(4) := #  -- 可以手动赋值
begin
  if pno < 5 then
    dbms_output.put _line('xxxxx');
  elsif pno  > 5 then
    dbms_output.put _line('xxx1221xx');
  else dbms_output.put _line('xxx11xx');
  end if;
end;
declare
  total number(4) := 0;
begin
  while total <= 100 loop
    total := total +1;
   end loop;
end;
declare -- 这种最常用的循环方式
  total number(4) := 0;
begin
  loop
      exit when total = 100;
      total := total +1;
   end loop;
end;
declare --适合连续的数值遍历
  total number(4) := 0;
begin
   for total in 1..100  loop  --从1到100
  dbms_output.put_line(total);
   end loop;
end;
declare
  prec emp%rowtype; -- 记录型变量
  cursor c1 is select * from emp; -- 定义游标
begin
  open c1;
    loop
        fetch c1 into prec; --从游标取值,之后,游标会自动下移一个
        exit when c1%notfound;
       dbms_output.put_line(prec.empno || '    ' ||prec.ename);
    end loop;
  close c1; --关闭游标
end;
--带有参数的游标
declare 
cursor c1(dno myemp.deptno%type) is select * from myemp t where t.deptno = dno;
prec myemp%rowtype;
begin
  open c1(10); -- 打开游标,10是传参
  loop
    fetch c1 into prec;
    exit when c1%notfound;
    update myemp t set t.sal = t.asl + 1000 wheret.empno = prec.empno;
 end loop;
close c1;
commit; --因为只update一条,所以commit可以放在后面
end; 
decalre 
  pnum number(4) := 5;
  cursor c1 id select *from emp t where t.deptno = 50;
  no_data exception;  --自定义异常
begin
  open c1;
  loop
      fetch c1 into prec;
      if c1%notfound then
            raise no_data; --抛出异常,用raise!
      end if;
  end loop

  pnum := pnum/0;  -- 发生异常
exception
  when no_data then
      dbms_output.put_line('自定义的异常抛出');
  when zero_divide then
    dbms_output.put_line('被0除');
  when ..... then .....
    ............
end;
create or replace procedure helloworld
as
begin
  dbms_output.put_line('xxxxx');
end;
begin
  helloworld;
end;  
declare --带有输入,输出参数的存储过程
  create or replace procedure addsal(pno in myemp.empno%type,ysal out number) as
--定义变量
  prec myemp%rowtype;
  psal emp.sal%type;
  
begin
  select *  into prec from myemp t where t.empno = pno;
  update myemp t set t.sal  = t.sal + 100 where t.empno = pno;
end;--最后在调用存储过程时,输入pno,就可以了。一般在调用时进行commit,不在存储过程内部进行commit;
decalre  
 ysal number;
begin
   countysal(222,ysal); --(输入,输出)
   dbms_output.put_line(ysal);
end;
create or replace function xxxx(输入参数) return 数据类型 is
begin
  return (变量名);
end xxxx;
create or replace trigger insertpt
before insert on person  --person是表名
begin   
  dbms_output.put_line('ssssssssss');
end insertpt;
create or replace trigger insertpt  --有条件的触发器
before insert on person  --person是表名
declare 
  cruday varchar2(10);--定义字符串 要给长度!
begin   
  select  to_char(sysdate,'day')  into cruday  from dual;
  if cruday = '星期三' then
      raise_application_erro(-20001,'星期三不插入数据');
  end if;
end insertpt;
create or replace trigger xxx
  before update of sal on myemp -- sal是列名
  for each row
begin
  if :new.sal <= :old.sal then
      raise_application_error(-20002,'涨工资后不能比之前低');
  end if;
end xxx;
上一篇 下一篇

猜你喜欢

热点阅读