oracle9I

来源:互联网 发布:英国现存贵族 知乎 编辑:程序博客网 时间:2024/05/21 12:44

1. 函数 :create function get_sal(va number)
               return number is  //定义输出参数
                        salary number(6,2)
              begin
                    select sal into salary from emp where **   //把查询的值赋给salary
                    return salary;   //注意上面只能返回一个值。
             end;
              函数有参数,输入参数va 输出参数 salary

2 过程 :create or replace procedure update_sal(name varchar2,val number) is
                 bengin
                          update emp set nu=number where na=name;
               end;  
               过程有参数,但是没有输出参数。

3 游标:游标的使用主要是用来取出很多记录
          declare
              cursor emp_cursor is select sal from **;  //定义游标为select后面的语句
             emp_recode emp_cursor%rowtype;     //定义字段类型
           begin
               open emp_cursor;   //打开游标
               loop //开始循环
                    fetch emp_cursor into emp_record;  //取出游标查询出来的记录存入
                exit when emp_cursor%notfound;  //退出条件
               if emp_recode.sal <2000 then  
                    update emp set sal=sal*1.1 where current of emp_cursor;
               end if;
         end loop;
      end;

4 异常:exception
           declare
                  name varchar(10);  //定义变量
           begin
                    select na into name from emp where **; //取之
                     dbms_output.put_line(name);//输出
          exception  //定义异常
                    when no_data_found then  // 没有数据异常
                     dbms_output.put_line('");
            end;
5 包:package
               create package pac is
                 //里面包含函数和过程 使用包里面的函数和过程是要用  包名.函数  包名.过程
               end;
6 触发器:
    create trigger update_ca afire update of  name on emp for each row // 定义触发器在emp表上的name字段的更新操作
               update emp set name=:new.name where name=:old.name;
  end;
     // 此处用的=: 而不是=

7 oracle 变量:
        oracle的变量分为普通的变量如:NUMBER,VARCHAR,VARCHAR2,BOLG,CURSOR,EXCEPTION
               还包括一些不普通的变量如:RECORD ,TABLE, OBJECT
               RECODE :
                              declear
                               type emp_record is record(    //定义一个record的类型
                               name emp.name%type,
                               val   mep.name%type,
                               );
                              val_record emp_record;          //声明一个emp_record的变量
             begin
                     select name,val into val_record from emp;  //赋值
            end;
            
              TABLE:
                   declare
                           type ename_table_type is table of emp.name%type index by binary_integer;
                  ename_table  ename_table_type;
                   begin
                         select ename into ename_table(-1) from emp
                   end;

8     oracle 的字符串连接用“||”
9     to_date('字符串A','字符串B') 将把字符串A按照字符串B的格式生产
10   oralce 查询语句中的like命令
               1. “-”表示一个字符
               2. 如果需要转移 需要使用escape 如 select * from emp where name like‘%a-%’escape ‘a’;
               结果讲查询出类似  fl-ay 的字符
11  多记录查询:
             insert /*+APPEND */  INTO emp select * from emp1 where **
          /* +append*/ 可以要也可以不要,但是当大量数据是要速度快。
12  多表插入
           insert ALL
                    when name=1 then into emp1   // 当查询出的字段值为1 则插入emp1
                     when name=2 then into emp2 //   。。。。。。。。。2 。。。emp2
                     when name=3 then into emp3 //
                     else into emp4 //   否则插入emp4
          select * from emp;
13 更新操作
         update set (id,name)=(select a,b from emp1 where a=123) where id=123

原创粉丝点击