您的位置:首页 > 数据库

PLSQL总结——15.DML事务

2012-02-15 16:21 155 查看
/*
create table EMPLOYEE
(
employee_number NUMBER,
employee_name   VARCHAR2(30),
salary          NUMBER(7,2),
department_id   NUMBER
)
*/
declare
/*显示表记录*/
procedure show_rec is
cursor c1 is
select * from employee;
begin
for rec in c1
loop
/*假如第一条,输出头*/
if c1%rowcount = 1
then
dbms_output.put_line('--------------------------------------------------');
dbms_output.put_line('employee_number|employee_name|salary|department_id');
end if;
dbms_output.put_line(rpad(rec.employee_number, 15, ' ') || '|' ||
rpad(rec.employee_name, 13, ' ') || '|' ||
rpad(rec.salary, 6, ' ') || '|' ||
rpad(rec.department_id, 13, ' '));
end loop;
dbms_output.put_line('--------------------------------------------------');
end show_rec;

begin

dbms_output.put_line('before update record...');
show_rec; --显示表记录

/*插入多条记录,复制多一倍数据*/
insert into employee
select * from employee;

savepoint a; --保存点

/*删除表的重复记录*/
delete employee
where rowid not in
(select max(rowid)
from employee
group by employee_number, employee_name, salary, department_id);

dbms_output.put_line('delete count : ' || sql%rowcount);
dbms_output.put_line('');

/*更新表,让工资变为1000*/
<<update_block>>
declare
employee_rec employee%rowtype;
update_row   pls_integer := 0;
cursor c1 is
select * from employee for update;
begin
for rec in c1
loop
employee_rec        := rec;
employee_rec.salary := 1000;
update employee set row = employee_rec where current of c1;
update_row := update_row + sql%rowcount;
end loop;
dbms_output.put_line(update_row || ' records were updated!');
dbms_output.put_line('');
end update_block;

commit;

dbms_output.put_line('after update record...');

show_rec; --显示表记录
exception
when others then
rollback to a; --返回保存点a
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: