使用Oracle中的游标选择,比较和更新

时间:2015-01-25 15:28:42

标签: sql oracle oracle11g cursor

我有两张桌子。

Customer_Master有3列c_idc_addrc_trans Customer_Address有3列c_idc_addrpr

我将Customer_Master提取到Cursor C1并将Customer_address提取到Cursor C2中。光标中的Select语句C2具有Where条件,该条件来自从光标C1获取的列c_idc_addr

Cursor C1 Select Statement: Select c_trans, c_id, c_addr, from customer_master

c_trans是主键。不是空的和独特的

光标C2选择参数:

Select pr from customer_address where c_id = cid and c_addr = cad

pr仅包含值True或False。

现在,我必须检查以查看天气pr包含值'True'。如果是真的,就不必做任何事情。

如果为false,则必须更新c_addr customer_master customer_address . c_addr,其值pr`Update customer_master Set c_addr = (select c_addr from customer_address where pr = 'TRUE' and c_id = cid) where c_trans = ctrans` 为真

`declare
cid number;
cadd number;
ctras number;
cr varchar(2);
cad number;
cursor c1 IS
select c_tras, c_id, c_add from customer_master;
cursor c2 IS
select c_address, cr from customer_address where c_id = cid;
begin
open c1;
open c2;
LOOP
fetch c1 into ctras, cid, cadd;
fetch c2 into cad, cr;
if cr='N'
THEN
update customer_master set c_address = (select c_address from customer_address where cr = 'Y' and c_id = cid) where c_tras = ctras;
END IF;
END LOOP;
END;`

如何实现这一点。

编辑:

我的代码;

{{1}}

1 个答案:

答案 0 :(得分:0)

这很完美。

declare
cid number;
cadd number;
ctras number;
pr varchar(2);
vad number;
cursor c1 IS
select ac_tras, cust_id, cust_addr from customer_master;
cursor c2 IS
select pr_adr from customer_address where cust_id = cid and cust_addr = cadd;
BEGIN
open c1;
LOOP
fetch c1 into ctras, cid, cadd;
EXIT WHEN C1%NOTFOUND;
OPEN c2;
LOOP
fetch c2 into pr;
if pr='Y'
THEN EXIT ;
ELSE
UPDATE customer_master 
set cust_addr = (select cust_addr from customer_address where pr_adr = 'Y' and cust_id = cid) where ac_tras = ctras;
END IF;
EXIT WHEN C2%NOTFOUND;
END LOOP;
Close C2;
END LOOP;
CLOSE C1;
END;