--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;