基本语法
*********************************************************************************************************
1. 打印员工号为186的员工的last_name值
declare
v_last_name employees.last_name%type;
begin
select last_name into v_last_name
from employees
where EMPLOYEE_ID=186;
dbms_output.put_line(v_last_name);
end;
----------------------------------------------------------
2. 打印员工号为186的员工的last_name,email,salary,job_id的值
declare
v_last_name employees.last_name%type;
v_email employees.email%type;
v_salary employees.salary%type;
v_job_id employees.job_id%type;
begin
select last_name,email,salary,job_id
into v_last_name,v_email,v_salary,v_job_id
from employees
where EMPLOYEE_ID=186;
dbms_output.put_line(v_last_name);
dbms_output.put_line(v_email);
dbms_output.put_line(v_salary);
dbms_output.put_line(v_job_id);
end;
----------------------------------------------------------------
记录类型
----------------------------------------------------------------
3.1 自定义记录类型,包括:姓名、员工号。给姓名、员工号赋值,并打印输出
declare
-- 定义记录的类型
type test_rec is record(
name varchar2(10),
id number(4)
);
v_record test_rec; --定义record类型的变量
begin
-- 对变量赋值
v_record.name :=‘tom‘;
v_record.id :=123;
dbms_output.put_line(v_record.name||‘,‘||v_record.id);
end;
3.2 自定义记录类型,打印员工号186的员工的last_name, email, salary, job_id
declare
-- 定义记录的类型
type test_rec is record(
last_name varchar2(30),
email varchar2(30),
salary number(8,2),
job_id varchar(10)
);
employees_record test_rec; --定义record类型的变量
begin
-- 对变量赋值
select last_name,email,salary,job_id
into employees_record.last_name,
employees_record.email,
employees_record.salary,
employees_record.job_id
from employees
where EMPLOYEE_ID=186;
dbms_output.put_line(employees_record.last_name
||‘,‘||employees_record.email
||‘,‘||employees_record.salary
||‘,‘||employees_record.job_id
);
end;
4. 定义一个记录类型,并使用 %type 定义变量,动态的获取数据的声明类型,打印员工号186的员工的last_name, email, salary, job_id
declare
-- 定义记录的类型
type test_rec is record(
last_name employees.last_name%type,
email employees.email%type,
salary employees.salary%type,
job_id employees.job_id%type
);
employees_record test_rec; --定义record类型的变量
begin
-- 对变量赋值
select last_name,email,salary,job_id
into employees_record.last_name,
employees_record.email,
employees_record.salary,
employees_record.job_id
from employees
where EMPLOYEE_ID=186;
dbms_output.put_line(employees_record.last_name
||‘,‘||employees_record.email
||‘,‘||employees_record.salary
||‘,‘||employees_record.job_id
);
end;
5. 使用 %rowtype ,打印员工号186的员工的全部信息
declare
v_employees employees%rowtype; --定义record类型的变量
begin
-- 对变量赋值
select *
into v_employees
from employees
where EMPLOYEE_ID=186;
dbms_output.put_line(v_employees.last_name
||‘,‘||v_employees.email
||‘,‘||v_employees.salary
||‘,‘||v_employees.job_id
);
end;
----------------------------------
6.1 使用赋值符号位变量进行赋值,通过变量实现查询186员工的全部信息
declare
type dep_table_type is table of employees%rowtype;
my_dep_table dep_table_type :=dep_table_type();
begin
my_dep_table.extend(5);
for i in 1..5 loop
select * into my_dep_table(i)
from employees
where EMPLOYEE_ID=186;
end loop;
dbms_output.put_line(my_dep_table.count());
dbms_output.put_line(my_dep_table(1).EMPLOYEE_ID
);
dbms_output.put_line(my_dep_table(1).FIRST_NAME
);
dbms_output.put_line(my_dep_table(1).LAST_NAME
);
dbms_output.put_line(my_dep_table(1).PHONE_NUMBER
);
dbms_output.put_line(my_dep_table(1).HIRE_DATE
);
dbms_output.put_line(my_dep_table(1).JOB_ID
);
dbms_output.put_line(my_dep_table(1).SALARY
);
dbms_output.put_line(my_dep_table(1).COMMISSION_PCT
);
dbms_output.put_line(my_dep_table(1).MANAGER_ID
);
dbms_output.put_line(my_dep_table(1).DEPARTMENT_ID
);
end;
-------------------
6.2 通过变量实现DELETE、INSERT、UPDATE等操作
declare
v_empno number(4) :=8889;
v_ename varchar2(20) := ‘zhangsan‘;
begin
insert into emp(empno,ename,hiredate) values(v_empno,v_ename,to_date(‘2019-04-22‘,‘yyyy-mm-dd‘));
--delete from emp where empno=v_empno;
--update emp set ename=‘张三‘ where empno=v_empno;
commit;
end;
补充:使用嵌套表类型,查询部门号在110-200之间的全部部门信息
declare
type dep_table_type is table of departments%rowtype;
my_dep_table dep_table_type :=dep_table_type();
begin
my_dep_table.extend(10);
for i in 1..10 loop
select * into my_dep_table(i)
from departments
where department_id=100+10*i;
end loop;
dbms_output.put_line(my_dep_table.count());
dbms_output.put_line(my_dep_table(1).department_id
);
dbms_output.put_line(my_dep_table(2).department_id
);
dbms_output.put_line(my_dep_table(3).department_id
);
dbms_output.put_line(my_dep_table(4).department_id
);
dbms_output.put_line(my_dep_table(5).department_id
);
dbms_output.put_line(my_dep_table(6).department_id
);
dbms_output.put_line(my_dep_table(7).department_id
);
dbms_output.put_line(my_dep_table(8).department_id
);
dbms_output.put_line(my_dep_table(9).department_id
);
dbms_output.put_line(my_dep_table(10).department_id
);
原文地址:https://www.cnblogs.com/zxrxzw/p/10754271.html