stcenter

1.近期写一些的plsql 下面是存储过程,供自己参考:

create or replace procedure procedureName
  as
  --编号ID
  v_id varchar2(20);
  
  --名称
  v_name varchar2(32);

  --属性
  v_a number(32);
  v_b number(32):=1000;

  --结果
  v_result number(32);
  
    begin
        /* 循环生成11-65的数字*/
        for i in 1..10 loop
       
         /* 遍历插入*/
        loop
        
        --查询数据库存在的数据量
        select count(1) into v_a from student where id like i||\'0000%\';  
        
        --判断辖区数据库实际存在的数据与预想插入的数据的大小
         if v_a>= v_b then
            --如果大于等于退出
            exit;
           end if;
           
         ---获取6位毫秒数
         select to_char(systimestamp, \'ff\') into var_now_ff from dual;
         
         -- 拼接dcl_io_decl编号ID
         v_id :=to_char(i)||\'03344\'||var_now_ff;
         
         --根据ID查询记录数
         select count(1) into v_result from student where id =\'\'||var_decl_id||\'\';
         
         --判断记录数是否为0
          if v_result=0 then
           --插入数据
               insert student values (v_id,v_name);
           end if;
       
           end loop;
     end loop;
    end;

2.游标基本用法:

create or replace procedure procedureName
  as
  --编号ID
  v_id varchar2(20);
  --名称
  v_name varchar2(32);
  --游标声明
  cursor c_list is select name from student
  
  begin
    --打开游标
      open c_list;
          for c in c_list loop
              fetch c_list into v_name;
              dbms_output.put_line(v_name);
            end loop;
        --关闭游标
        close c_list; 
  end;
create or replace procedure procedureName
  as
  --编号ID
  v_id varchar2(20);
  --名称
  v_name varchar2(32);
  --游标声明
  cursor c_list is select name from student
  
  begin
    --打开游标
      open c_list;
           loop
              fetch c_list into v_name;
              exit when c_list%notfund;
              dbms_output.put_line(v_name);
            end loop;
        --关闭游标
        if c_list%isopen then
          close c_list; 
        end if;
  end;

3.动态游标色使用:

create or replace procedure procedureName
  as
  --编号ID
  v_id varchar2(20);
  --名称
  v_name varchar2(32);
  --游标类型
  type query_curtype is ref cursor;
  --游标变量
  v_cursor query_curtype;
  --sql
  v_sql varchar2(200);
  
  begin
      v_sql:= select  name  from student where id = \'\'v_id\'\';
    --打开游标
      open v_cursor for v_sql;
           loop
              fetch c_list into v_name;
              exit when c_list%notfund;
              dbms_output.put_line(v_name);
            end loop;
        --关闭游标
        if c_list%isopen then
          close c_list; 
        end if;
  end;


 

 

分类:

技术点:

相关文章: