PL/SQL

Posted lfdingye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PL/SQL相关的知识,希望对你有一定的参考价值。

--declare
  --声明的变量、类型、游标
begin
  --程序的执行部分(类似于java里的main方法)
  dbms_output.put_line(Hello World!);
--exception
  --针对begin块中出现的异常,提供处理的机制
  --when...then...
  --when...then...
end;



declare
  v_sal varchar(20);
begin
  select salary into v_sal from employees where employee_id = 100;
  dbms_output.put_line(v_sal);
end;

declare
  v_sal varchar(20);
  v_email varchar2(20);
  v_hire_date date;
begin
  select salary,email,hire_date into v_sal,v_email,v_hire_date from employees where employee_id = 100;
  dbms_output.put_line(v_sal||,||v_email||,||v_hire_date);
end;


declare 
  v_sal employees.salary%type;
  v_email employees.email%type;
  v_hire_date employees.hire_date%type;
begin
  select salary,email,hire_date into v_sal,v_email,v_hire_date from employees where employee_id = 100;
  dbms_output.put_line(v_sal||,||v_email||,||v_hire_date);
end;


declare
  v_sal employees.salary%type;
  v_email employees.email%type;
  v_hire_date employees.hire_date%type;
begin
  select salary,email,hire_date into v_sal,v_email,v_hire_date from employees where employee_id = 100;
  dbms_output.put_line(v_sal||,||v_email||,||v_hire_date);
end;


declare
  type emp_record is record(
    v_sal employees.salary%type,
    v_email employees.email%type,
    v_hire_date employees.hire_date%type
  );
  v_emp_record emp_record;
begin
  select salary,email,hire_date into v_emp_record from employees where employee_id = 100;
  dbms_output.put_line(v_emp_record.v_sal||,||v_emp_record.v_email||,||v_emp_record.v_hire_date);
end;


declare
--变量、记录类型等的声明
    v_salary number(8,2) := 0;
begin
--程序的执行部分
    select  salary into v_salary 
    from    employees 
    where   employee_id = 123;
    dbms_output.put_line(salary: || v_salary);
--exception
--程序的异常处理
end;

declare
  type salary_record is record(
     v_name varchar2(20),
     v_salary number(10,2)
  );
  v_sal_record salary_record;
begin
  v_sal_record.v_name :=刘德华;
  v_sal_record.v_salary :=120000;
  dbms_output.put_line(name: ||v_sal_record.v_name || salary: || v_sal_record.v_salary);
end;


declare
    v_sal employees.salary%type;
begin
    select salary into v_sal from employees where employee_id = 148;
    
    if v_sal >= 10000 then dbms_output.put_line(salary >= 10000);
    elsif v_sal >= 5000 then dbms_output.put_line(5000 <= salary < 10000);
    else dbms_output.put_line(salary<5000);
    end if;
end;

--case when只能写具体的值,不能有具体的范围
declare 
     v_sal employees.salary%type;
     v_temp varchar2(30);
begin
    select salary into v_sal from employees where employee_id = 150;
    v_temp:=
    case trunc(v_sal/50000) when 0 then salary < 5000
                            when 1 then 5000 <= salary < 10000
                            else salary >= 10000
    end;
    dbms_output.put_line(v_sal || , || v_temp);
end;




declare 
    v_job_id varchar2(20);
    v_temp varchar2(20);
begin
    select job_id into v_job_id from employees where employee_id = 122;
    v_temp:=
           case v_job_id when IT_PROG then A
                         when AC_MGT then B
                         when AC_ACCOUNT then C
                         else D
    end;
    dbms_output.put_line(v_job_id || , || v_temp);
end;



declare
    v_i number(5):=1;
begin
    loop
       dbms_output.put_line(v_i);
       v_i := v_i+1; 
    exit when v_i >=100 ;
    end loop;
end;
    

 

以上是关于PL/SQL的主要内容,如果未能解决你的问题,请参考以下文章

PL/SQL包(package)操作实例讲解

SQL记录-PLSQL-DBMS输出

异常后停止 PL/SQL 代码(PL/SQL、ORACLE)

使用PL/SQL developer概览图剖析pl/sql代码

使用PL/Scope分析PL/SQL代码

关于PL/SQL Developer13 代码自动对齐(PL/SQL Beautifier美化)功能