oracle pl/sql 1

来源:互联网 发布:php new class 返回值 编辑:程序博客网 时间:2024/06/11 03:08
 

/*create table temp_tab(
       id number(11) primary key
);*/

--使用for xx in xxxlower..xxxheight loop  [xxxxxx] end loop;
/*declare
  v_id number(2);
begin
  for v_id in 2..10 loop
    insert into temp_tab values(v_id);
    end loop;
end;*/

--CASE 语句
/*declare
    v_num number:=1;
    v_result varchar2(64);
begin
  case v_num
    when 1 then v_result:='星期1';
    when 2 then v_result:='星期2';
    when 3 then v_result:='星期3';
    when 4 then v_result:='星期4';
  end case;
  dbms_output.put_line(v_result);
end;*/


--动态执行   DDL数据定义语言 (reate ,drop ,alter)
/*declare
 
begin
   execute immediate 'create table temp_table(id number(11),name varchar2(20))';  
end;*/
/*select * from temp_table*/

--动态执行sql 赋值 into 变量名
/*declare
   v_time varchar2(20);    
begin
   select to_char(sysdate,'dd_mm_dd_ day') into v_time from dual;
   dbms_output.put_line('当前日期是:' || v_time);  
end; */

--使用变量 (占位符 :x,:y)
/*declare
  id    number;
  plsql varchar2(100);
begin
  id:=11;
  plsql:='insert into temp_tab values(:1)';
  execute immediate plsql using id;
end;*/

--自定义 异常 处理
/*declare
temp_ex exception;--定义异常
t_num number;
begin
  select count(1) into t_num from temp_tab where id=1;
  if t_num=0 then
    --触发异常 (跳到执行处理异常)
    raise temp_ex;   
  end if; 
  dbms_output.put_line('该用户不存在'||t_num);
 
  --处理异常
  exception
    when temp_ex then
       dbms_output.put_line('该用户已经存在');
end;*/

--使用 游标  抓获 select 结果  【声明游标,开启游标,提取游标,关闭游标】
/*declare
     id number;
     --声明游标
     cursor t_cursor is select id from temp_tab where id=1;
begin
  --打开游标
  open t_cursor;
  --判断游标是否开启
  if t_cursor%notfound then
    dbms_output.put_line('没有找到相应的记录');
  else
    --从游标中读取记录
    fetch t_cursor into id;
    dbms_output.put_line('id = '||id);
  end if;
end;*/

--游标的循环读取(for xx in xx loop xxxxxx end loop;)游标开启,关闭会自动进行
/*
declare
 id number;
 cursor id_cursor is select * from temp_tab;
begin
  for tid in id_cursor loop
      id:=tid.id;
      dbms_output.put_line('id : '||id);
  end loop; 
end;*/

--游标的循环读取2(for xx in xx loop xxxxxx end loop;)游标开启,关闭会自动进行

declare
 
 id number;
 cursor id_cursor is select id from temp_tab;
begin
  for tid in id_cursor loop
      id:=tid.id;
      dbms_output.put_line('id : '||id);
  end loop; 
end;

 

 

 

原创粉丝点击