sql用insert into 插入的表数据,当select语句的结果变化时,怎么同步#1的数据
sql用insertinto#1(1,2,3,4)select1,2,3,4from。。。插入的表数据,当select语句的结果变化时,怎么同步#1的数据...
sql用insert into #1 (1,2,3,4) select 1,2,3,4from。。。插入的表数据,当select语句的结果变化时,怎么同步#1的数据
展开
2个回答
展开全部
你的意思是我查询tab2里的数据插入到tab1中,如果tab2中数据有变化 插入对应tab1的数据也跟着变化是吗?
个人建议 你可以写个存储过程,当select tab2数据返回一个参数,如果参数改变 触发insert tab1
以下是我之前写过类似的存储过程 其中字段名你自己更改下 应该对你有帮助
create or replace procedure pro_supplier(v_id in varchar2) as
sup_id varchar(800);
v_sort number(10);
v_num number(10);
begin
select f4030.id into sup_id from formmain_4030 f4030
where f4030.field0001 = (select f4697.field0001 from formmain_4697 f4697 where f4697.id =
v_id);
select max(f4031.sort) into v_sort from formson_4031 f4031 where f4031.formmain_id =
sup_id;
select max(f4031.field0028) into v_num from formson_4031 f4031 where f4031.formmain_id =
sup_id;
update formson_4031 f4031 set (f4031.field0031, f4031.field0034) = (select f4698.field0033,
f4698.field0034 from formson_4698 f4698
where f4698.formmain_id = v_id
and f4698.field0032 = f4031.field0030)
where f4031.formmain_id = sup_id
and exists (select 1
from formson_4698 f4698
where f4698.formmain_id = v_id
and f4698.field0032 = f4031.field0030);
commit;
insert into formson_4031
(id,
formmain_id,
sort,
field0028,
field0029,
field0030,
field0031,
field0033,
field0034)
select f4699.id,
sup_id,
to_number(v_sort + f4699.sort),
to_number(v_num + f4699.field0036),
f4699.field0037,
f4699.field0038,
f4699.field0039,
f4699.field0041,
f4699.field0040
from formson_4699 f4699
where f4699.formmain_id = v_id;
commit;
end;
个人建议 你可以写个存储过程,当select tab2数据返回一个参数,如果参数改变 触发insert tab1
以下是我之前写过类似的存储过程 其中字段名你自己更改下 应该对你有帮助
create or replace procedure pro_supplier(v_id in varchar2) as
sup_id varchar(800);
v_sort number(10);
v_num number(10);
begin
select f4030.id into sup_id from formmain_4030 f4030
where f4030.field0001 = (select f4697.field0001 from formmain_4697 f4697 where f4697.id =
v_id);
select max(f4031.sort) into v_sort from formson_4031 f4031 where f4031.formmain_id =
sup_id;
select max(f4031.field0028) into v_num from formson_4031 f4031 where f4031.formmain_id =
sup_id;
update formson_4031 f4031 set (f4031.field0031, f4031.field0034) = (select f4698.field0033,
f4698.field0034 from formson_4698 f4698
where f4698.formmain_id = v_id
and f4698.field0032 = f4031.field0030)
where f4031.formmain_id = sup_id
and exists (select 1
from formson_4698 f4698
where f4698.formmain_id = v_id
and f4698.field0032 = f4031.field0030);
commit;
insert into formson_4031
(id,
formmain_id,
sort,
field0028,
field0029,
field0030,
field0031,
field0033,
field0034)
select f4699.id,
sup_id,
to_number(v_sort + f4699.sort),
to_number(v_num + f4699.field0036),
f4699.field0037,
f4699.field0038,
f4699.field0039,
f4699.field0041,
f4699.field0040
from formson_4699 f4699
where f4699.formmain_id = v_id;
commit;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询