plsql匿名塊

PL/SQL匿名塊詳解

摘要:

PL/SQL是Oracle下的腳本開發語言,是爲了解決數據下一些複雜的應用而設計的。一些複雜的應用,僅靠一條SQL語句並不能完成,爲了解決這個問題,各個數據庫產品都設計了自己的腳本編程語言,Oracle下叫做PL/SQL,SQL Server下叫做Transact SQL。

PL/SQL單行註釋使用--,多行註釋使用/**/。PL/SQL中的變量類型主要有一下幾種:binary_integer: 整數,主要用來技數而不是用來表示字段類型number: 數字類型char: 定長字符串varchar2: 變長字符串date: 日期long:長字符串,最長2GBboolean: 布爾類型,可以取值爲true,false和null值。 匿名塊是能夠動態地創建和執行過程代碼的PL/SQL結構,而不需要以持久化的方式將代碼作爲數據庫對象儲存在系統目錄中。
--HelloWorld開始
set serveroutput on;
 
begin
   dbms_output.put_line('Hello World!!!');
end;
/
 
 
--最簡單的語句塊
declare
  v_word varchar(20);
begin
  v_word:='Hello World!!!';
  dbms_output.put_line(v_word);
end;
/
 
 
--語句塊的組成
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;
/
 
 
--變量的聲明
declare
  v_temp number(1);
  v_count binary_integer :=0;
  v_sal number(7,2):=3000.00;
  v_date date:=sysdate;
  v_pi_constrant number(3,2):=3.14;
  v_valid boolean :=false;
  v_name varchar2(20) not null :='My Name';
begin
  dbms_output.put_line('v_temp value:' || v_temp);
end;
/
 
 
--變量聲明,使用%type屬性
declare
  v_empno number(4);
  v_empno2 emp.empno%type;
  v_empno3 v_empno2%type;
begin
  dbms_output.put_line('test...');
end;
/
 
 
--簡單變量賦值
declare
  v_name varchar2(20);
  v_sal number(7,2);
  v_sal2 number(7,2);
  v_valid boolean := false;
  v_date date;
begin
  v_name := 'My Name';
  v_sal := 23.77;
  v_sal2 := 23.77;
  v_valid := (v_sal=v_sal2);
  v_date := to_date('2010-08-04 22:58:58','YYYY-MM-DD HH24-MI-SS');
end;
/
 
 
--table變量類型
declare
  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(1) := 7839;
  v_empnos(-1) := 9999;
  dbms_output.put_line(v_empnos(-1));
end;
/
 
 
--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 := 'dname';
  v_temp.loc := 'BJ';
  dbms_output.put_line(v_temp.deptno || ' ' || v_temp.dname);
end;
/
 
 
--使用%rowtype 聲明record變量
declare
  v_temp dept%rowtype;
begin
  v_temp.deptno := 50;
  v_temp.dname := 'dname';
  v_temp.loc :='BJ';
  dbms_output.put_line(v_temp.deptno || ' ' || v_temp.dname);
end;
/
 
 
--SQL語句應用
declare
  v_ename emp.ename%type;
  v_sal emp.sal%type;
begin
  select ename,sal into v_ename,v_sal from emp where empno=7900;
  --select ename,sal into v_ename,v_sal from emp where deptno=30;
  dbms_output.put_line(v_ename || ' ' || v_sal);
end;
/
 
 
--SQL語句應用
declare
  v_emp emp%rowtype;
begin
  select * into v_emp from emp where empno=7369;
  dbms_output.put_line(v_emp.ename);
end;
/
 
 
--SQL語句應用
create table dept2(
  deptno number,
  dname varchar2(50),
  loc varchar2(50)
);
declare
  v_deptno dept.deptno%type := 50;
  v_dname dept.dname%type :='My Name';
  v_loc dept.loc%type := 'BJ';
begin
  insert into dept2 values(v_deptno,v_dname,v_loc);
end;
/
select * from dept2;
--drop table dept2;
 
 
--SQL語句應用
declare
  v_deptno dept2.deptno%type := 30;
  v_count number;
begin
  select deptno into v_deptno from dept2 where deptno=50;
  select count(*) into v_count from dept2;
  dbms_output.put_line('deptno:' || v_deptno);
  dbms_output.put_line('count:' || v_count);
  dbms_output.put_line(sql%rowcount || '條記錄被影響...');
end;
/
 
 
--使用DDL語句
begin
execute immediate 'create table T(nnn varchar2(20))';
end;
/
drop table T;
 
 
--選擇語句
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;
/
 
 
--循環語句
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;
/
 
 
--錯誤處理
declare
  v_temp number(4);
begin
  select empno into v_temp from emp where deptno=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('沒有數據...');
  when others then
    dbms_output.put_line('error...');
end;
/
 
 
--遊標
declare
  cursor c is select * from emp;
  v_emp c%rowtype;
begin
  open c;
  fetch c into v_emp;
  dbms_output.put_line(v_emp.ename);
  close c;
end;
/
 
 
--遊標
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;
/
 
 
--帶參數的遊標
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;
  --v_temp c%rowtype;
begin
  --open c(30,'clerk');
  for v_temp in c(30,'CLERK') loop
    dbms_output.put_line(v_temp.ename || ' ' || v_temp.sal);
  end loop;
end;
/
 
 
--可以更新的遊標
create table emp2 as(
   select * from emp
);
drop table emp2;
declare
  cursor c is select * from emp2 for update;
  --v_temp c%rowtype;
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;
/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章