使用存储过程读取源表数据到目标表

wildwolv 2018-03-05 03:23:50
源表cdc_test
目标表:test_source
select operation$,commit_timestamp$,id,name,mark from cdc_test
查询到一条数据:
OP COMMIT_TIMESTA ID NAME
-- -------------- ---------- ------------------------------
MARK
--------------------------------------------------
I 05-3月 -18 4 renqinglei4
aa4

存储过程的写法:
create or replace procedure A
is
begin
IF cdc_test.operation$='I' THEN
insert into test_source(id,name,mark,statrdate,enddate)
select t1.id,t1.name,t1.mark,t1.commit_timestamp$,'31-12月-9999' from cdc_test t1;
end if;

IF cdc_test.operation$='D' THEN
update test_source
set enddate=cdc_test.commit_timestamp$
where id=cdc_test.id
and cdc_test.operation$='D';
end if;

IF
cdc_test.operation$='UN' THEN
update test_source
set enddate=cdc_test.commit_timestamp$
where id=cdc_test.id
and enddate='31-12月-9999'
end if;

IF
cdc_test.operation$='UN' THEN
insert into test_source(id,name,mark,Startdate,enddate)
select t1.id,t1.name,t1.mark,t1.commit_timestamp$,'31-12月-9999' from cdc_test t1
end if;
COMMIT;
end;

执行存储过程无法读取源表的数据到目标表,请问是为什么?
...全文
553 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wildwolv 2018-03-06
  • 打赏
  • 举报
回复
我发现了,我把test_source表里的startdate名字写错了 谢谢各位!
卖水果的net 2018-03-05
  • 打赏
  • 举报
回复
后两个 if 内的语句,结尾没有写分号。
liu志坚 2018-03-05
  • 打赏
  • 举报
回复

先纠正错误,编译通过
liu志坚 2018-03-05
  • 打赏
  • 举报
回复
引用 4 楼 wildwolv 的回复:
为什么同样的sql我执行起来会报错呢?
存储过程编译失败了,你打开来看是啥错误
  • 打赏
  • 举报
回复
是把存储过程里面的 begin end 复制出来 用 declare 你前面过程的参数定义一下; begin 存储过程里面的 begin end 中间的; end 这种方式 进行调试
wildwolv 2018-03-05
  • 打赏
  • 举报
回复

为什么同样的sql我执行起来会报错呢?
liu志坚 2018-03-05
  • 打赏
  • 举报
回复
你存储过程估计都编译失败的。先调通,然后
begin
a;
end;

执行。

wildwolv 2018-03-05
  • 打赏
  • 举报
回复
按你的这个写法执行以后数据还是没有写过去
liu志坚 2018-03-05
  • 打赏
  • 举报
回复
你的存储过程的语法有问题,你参考这个改改: create or replace procedure A is begin for v_cur in (select * from cdc_test ) loop IF v_cur.operation$='I' THEN insert into test_source(id,name,mark,statrdate,enddate) select t1.id,t1.name,t1.mark,t1.commit_timestamp$,to_date('9999-12-31','YYYY-MM-DD') from cdc_test t1 where t1.id=v_cur.id; end if; COMMIT; end loop; end;

17,078

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧