Select ,在 Oracle 中使用游标进行比较和更新
Select , Compare and Update using cursors in Oracle
我有两张桌子。
Customer_Master
有 3 列 c_id
、c_addr
、c_trans
Customer_Address
有 3 列 c_id
、c_addr
、pr
我将 Customer_Master
提取到 Cursor C1
中,并将 Customer_address
提取到 Cursor C2
中。游标 C2
中的 Select 语句有一个 Where
条件,该条件来自从游标 C1 获取的列 c_id
和 c_addr
。
游标C1
Select语句:
Select c_trans, c_id, c_addr, from customer_master
c_trans
是主键。非空且唯一
光标C2
Select语句:
Select pr from customer_address where c_id = cid and c_addr = cad
pr
仅包含值 True 或 False。
现在,我必须检查天气 pr
是否包含值 'True'。如果为真,则无需执行任何操作。
如果为假,它必须用 customer_address . c_addr
的值更新 customer_master
的 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;`
这很完美。
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;
我有两张桌子。
Customer_Master
有 3 列 c_id
、c_addr
、c_trans
Customer_Address
有 3 列 c_id
、c_addr
、pr
我将 Customer_Master
提取到 Cursor C1
中,并将 Customer_address
提取到 Cursor C2
中。游标 C2
中的 Select 语句有一个 Where
条件,该条件来自从游标 C1 获取的列 c_id
和 c_addr
。
游标C1
Select语句:
Select c_trans, c_id, c_addr, from customer_master
c_trans
是主键。非空且唯一
光标C2
Select语句:
Select pr from customer_address where c_id = cid and c_addr = cad
pr
仅包含值 True 或 False。
现在,我必须检查天气 pr
是否包含值 'True'。如果为真,则无需执行任何操作。
如果为假,它必须用 customer_address . c_addr
的值更新 customer_master
的 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;`
这很完美。
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;