请教这样的存储过程怎么写?
两个表,表结构一样
BH,LX,DW,QFRQ(类型为date类型)
例如
TABLE_SJYA
0001 sl , 320010 2013/3/10 17:28:51
0002 sl , 320010 2013/3/10 17:28:51
0005 sl , 320010 2013/3/10 7:28:51
TABLE_SJYB
0001 sl , 320010 2013/3/10 17:28:51
0002 sl , 320010 2013/3/10 7:28:51
0006 sl , 320010 2013/3/10 17:28:51
根据传入的参数lx,dw,qfrq (格式是varchar2(6)YYYYMM,比如201303)为条件
分别找到A表和B表的数据,然后比较差异,把不同的数据放到C表
C表结构为
BH,LX,DW,QFRQ (格式也是YYYYMM)
比如上述例子中 传入的参数为sl,320010,201303 ,则会把0005,0006 放到C表里,结果为
0005 sl , 320010 201303
0006 sl , 320010 201303
------解决方案--------------------
with a as (
select '0001' sl, '320010', '201303'
from dual
union all
select '0002' sl, '320010', '201303'
from dual
union all
select '0005' sl, '320010', '201303' from dual),
b as (select '0001' sl, '320010', '201303'
from dual
union all
select '0002' sl, '320010', '201303'
from dual
union all
select '0006' sl, '320010', '201303' from dual)
(select * from a minus select * from b)
union
(select * from b minus select * from a)
------解决方案--------------------
求差异,那minus不就可以了~~
------解决方案--------------------
哈哈,itpub给你答复过了、、
insert into TABLE_c
SELECT BH, LX, DW, to_char(t.qfrq, 'yyyymm')
FROM (select *
from TABLE_SJYA
UNION
select * from TABLE_SJYb) t
where t.lx = 'sl'
and t.dw = '320010'
and to_char(t.qfrq, 'yyyymm') = '201303'
MINUS
SELECT BH, LX, DW, qfrq
FROM (select BH, LX, DW, to_char(t.qfrq, 'yyyymm') qfrq
from TABLE_SJYA t
INTERSECT
select BH, LX, DW, to_char(t.qfrq, 'yyyymm') from TABLE_SJYB t) t
where t.lx = 'sl'
and t.dw = '320010'
and qfrq = '201303'