[英]In DB2, perform an update based on insert for large number of rows
In DB2, I need to do an insert, then, using results/data from that insert, update a related table. 在DB2中,我需要执行一个插入操作,然后使用该插入操作中的结果/数据来更新相关表。 I need to do it on a million plus records and would prefer not to lock the entire database.
我需要对一百万条以上的记录进行处理,并且不希望锁定整个数据库。 So, 1) how do I 'couple' the insert and update statements?
因此,1)我如何“耦合”插入和更新语句? 2) how can I ensure the integrity of the transaction (without locking the whole she-bang)?
2)如何确保交易的完整性(不锁定整个交易)?
some pseudo-code should help clarify 一些伪代码应有助于澄清
STEP 1 第1步
insert into table1 (neededId, id) select DYNAMICVALUE, id from tableX where needed value is null
STEP 2 第2步
update table2 set neededId = (GET THE DYNAMIC VALUE JUST INSERTED) where id = (THE ID JUST INSERTED)
note: in table1, the ID col is not unique, so i can't just filter on that to find the new DYNAMICVALUE 注意:在表1中,ID col不是唯一的,因此我不能仅对其进行过滤以查找新的DYNAMICVALUE
This should be more clear (FTR, this works, but I don't like it, because I'd have to lock the tables to maintain integrity. Would be great it I could run these statements together, and allow the update to refer to the newAddressNumber value.) 这应该更清楚(FTR,这可行,但是我不喜欢它,因为我必须锁定表以保持完整性。我可以将这些语句一起运行,并允许更新引用来更好newAddressNumber值。)
/****RUNNING TOP INSERT FIRST****/*
--insert a new address for each order that does not have a address id
insert into addresses
(customerId, addressNumber, address)
select
cust.Id,
--get next available addressNumber
ifNull((select max(addy2.addressNumber) from addresses addy2 where addy2.customerId = cust.id),0) + 1 as newAddressNumber,
cust.address
from customers cust
where exists (
--find all customers with at least 1 order where addressNumber is null
select 1 from orders ord
where 1=1
and ord.customerId = cust.id
and ord.addressNumber is null
)
/*****RUNNING THIS UPDATE SECOND*****/
update orders ord1
set addressNumber = (
select max(addressNumber) from addresses addy3
where addy3.customerId = ord1.customerId
)
where 1=1
and ord1.addressNumber is null
IDENTITY_VAL_LOCAL函数是一个不确定性函数,它返回标识列的最新分配值,该赋值是由于使用VALUES子句的单个INSERT语句而发生的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.