当前位置: 代码迷 >> PB >> 怎么完成两个表数据的互相核对
  详细解决方案

怎么完成两个表数据的互相核对

热度:54   发布时间:2016-04-29 05:34:52.0
如何完成两个表数据的互相核对
我用的是oracle+pb,比如说,表1的结构是
a   b    
1   12 
2   25
1   44
表2的结构是
a   b    c
1   44
4   55
所有列的类型是varchar2.
将表1中a列和b列的数据与表2中a列和b列的数据逐个对比,若相等,则将表2的c列置为1.上例

中表2的第一行符合条件,置为1.比对结束,若表1中有的行在表2中没有找到,则将该行置为1.

上例中表1的第一行、第二行均符合条件,置为1.
请问在oracle数据库中如何实现?
前台用pb开发,用游标嵌套但是总是死机不知道为什么。
游标代码:
string a1
string b1

string a2
string b2
string c2

declare cur_1 cursor for select a1,b1 from 表1;
declare cur_2 cursor for select a2,b2,c2 from 表2;

open cur_1;
fetch cur_1 into :a1,:b1;
do while sqlca.sqlcode=0
open cur_2;
fetch cur_cz into :a2,:b2,:c2;
do while sqlca.sqlcode=0
    if a1 = a2  and b1=b2 then c1='1'
        loop
        close cur2;
loop
close cur_1;
没有报警,就是运行就死机。大家看下是什么原因?
还有如果在oracle里面写存储过程用pb调用可以么?
------解决方案--------------------
因为你外面那个while里面没有fetch cur_1 into :a1,:b1;
你这段代码去除cur_2来看就是:
declare cur_1 cursor for select a1,b1 from 表1;
declare cur_2 cursor for select a2,b2,c2 from 表2;

open cur_1;
fetch cur_1 into :a1,:b1;
do while sqlca.sqlcode=0

loop

就是一个死循环
close cur_1;
------解决方案--------------------
将表1中a列和b列的数据与表2中a列和b列的数据逐个对比,若相等,则将表2的c列置为1.上例
中表2的第一行符合条件,置为1.比对结束,若表1中有的行在表2中没有找到,则将该行置为1.
上例中表1的第一行、第二行均符合条件,置为1.

没看明白你的需求,找到置为1,没有找到也置为1?
------解决方案--------------------
你试试以下SQL是否满足你
表1的结构是
a   b    
1   12 
2   25
1   44
表2的结构是
a   b    c
1   44
4   55

以下代码为表1 和 表2有相同数据时,把表2中的C列置为1
update 表2 set c = '1' where exists ( select 1 from 表1 where a = 表2.a and b = 表2.b);

如果没有数据置为0,可以用这个SQL
update 表2 set c = '0' where not exists ( select 1 from 表1 where a = 表2.a and b = 表2.b)

------解决方案--------------------
对于表1的更新,可以考虑先把所有 c 置为 0,再直接用数据窗口更新,可能更简单。。
select a, b, c from table1
minus
select a, b, 0 as c from table2
得到结果行
1   12   0 
2   25   0
置1
  相关解决方案