当前位置:网站首页>PLSQL学习第三天
PLSQL学习第三天
2022-08-10 06:49:00 【loveforever__】
双表同步数据的oracle package使用触发器实现
package
create or replace package PKG_YYY is
procedure EMP_INSERT;
procedure EMP_SELECT;
procedure EMP_DELETE;
procedure EMP_UPDATE;
end PKG_YYY;
package body
create or replace package body PKG_YYY is
--增加数据procedure EMP_INSERT is
cursor cursor_insert is
select * from emp03;
v_all cursor_insert%rowtype;
begin
open cursor_insert;
loop
fetch cursor_insert into v_all;
exit when cursor_insert%notfound;
insert into emp03 values(v_all.EMPLOYEE_ID,v_all.LAST_NAME,v_all.SALARY,v_all.DEPARTMENT_ID,v_all.HIRE_DATE);
commit;
end loop;
close cursor_insert;
end EMP_INSERT;--更新工资低于 3000 的员工工资调为 3000
procedure EMP_UPDATE is
cursor cursor_update is
select employee_id,salary from emp01;
v_id emp03.employee_id%type;
v_sal emp03.salary%type;
begin
open cursor_update;
loop
fetch cursor_update into v_id,v_sal;
exit when cursor_update%notfound;
if v_sal <3000 then
update emp03 set salary =3000 where employee_id=v_id;
dbms_output.put_line('员工:'||v_id||'工资已经更新');
end if;
commit;
end loop;
dbms_output.put_line('一共有'||cursor_update%rowcount||'条数据被调正');
close cursor_update;
end EMP_UPDATE;
--查询前 10 名员工的信息。
procedure EMP_SELECT is
cursor cursor_select is
select last_name,salary from emp03
where rownum<11 order by salary;
v_name emp03.last_name%type;
v_sal emp03.salary%type;
begin
open cursor_select;
fetch cursor_select into v_name, v_sal;
while cursor_select %found loop
dbms_output.put_line(v_name||':'||v_sal);
fetch cursor_select into v_name, v_sal;
commit;
end loop;
close cursor_select;
end EMP_SELECT;--删除数据
procedure EMP_DELETE is
cursor cursor_delete is
select employee_id from emp03;
v_id emp03.employee_id%type;
e_deleteid_exception exception;
pragma exception_init(e_deleteid_exception,-2292);
begin
open cursor_delete;
fetch cursor_delete into v_id;
delete from employees
where employee_id=100;
exception
when e_deleteid_exception then dbms_output.put_line('违反完整性约束条件,不可删除此用户!');
close cursor_delete;
commit;
end EMP_DELETE;
end PKG_YYY;
触发器
create or replace trigger A_TRIGGER
after
insert or update or delete on emp03
for each row
begin
if inserting then
insert into emp04(employee_id,last_name,salary,department_id,hire_date)
values(:new.employee_id,:new.last_name,:new.salary,:new.department_id,:new.hire_date);
elsif updating then
update emp04 set salary =:new.salary
where employee_id=:new.employee_id;
elsif deleting then
delete from emp04 where employee_id= :new.employee_id;
end if;
end A_TRIGGER;
测试数据添加
declare
v_1 emp03.employee_id%type :=50;
v_2 emp03.last_name%type :='bill';
v_3 emp03.salary%type :=12340.00;
v_4 emp03.department_id%type :=20;
begin
insert into emp03(employee_id,last_name,salary,department_id,hire_date)
values(v_1,v_2,v_3,v_4,sysdate);
end;
测试 结果
边栏推荐
猜你喜欢
Nude speech - lying flat - brushing questions - big factory (several tips for Android interviews)
MySQL索引事务
About MongoDb query Decimal128 to BigDecimal problem
几行代码就可以把系统高崩溃;
MySQL之InnoDB引擎(六)
强化学习_03_表格方法实践(CartPole-v0 And MontoCarlo)
语法基础(判断语句)
阿里巴巴(中国)网络技术有限公司、测试开发笔试二面试题(附答案)
mysql之两阶段提交
ES13 - ES2022 - The 123rd ECMA Congress approves the ECMAScript 2022 language specification
随机推荐
ATH10传感器读取温湿度
椭圆曲线离散对数问题以及求解
u-boot ERROR: Failed to allocate 0x5c6f bytes below 0x17ffffff.Failed using fdt_high value
Chapter 11 Database Design Specifications [2. Index and Tuning] [MySQL Advanced]
C language file operation
BUUCTF笔记(web)
WooCommerce 安装和 rest api 使用
Big guy, when Oracle single-table incremental synchronization, the source database server takes up nearly 2g of memory. This is not normal, right?
2022 Henan Mengxin League (fifth) game: University of Information Engineering H - Xiao Ming drinking milk tea
ES13 - ES2022 - 第 123 届 ECMA 大会批准了 ECMAScript 2022 语言规范
大佬,oracle单表增量同步时候源库服务器额外占用内存近2g,这不正常吧
各位大佬 oracle cdc 默认配置 偶发会30秒才抓取到数据 这个怎么优化啊
浅谈C语言整型数据的存储
1413. Stepwise Summation to Get Minimum Positive Numbers
语法基础(判断语句)
动态规划——从0-1背包问题到leetcode正则匹配
941 · 滑动拼图
SCS【2】单细胞转录组 之 cellranger
I would like to ask you guys, when FLink SQL reads the source, specify the time field of the watermark. If the specified field is in the grid
WooCommerce installation and rest api usage