我需要一些帮助来编写更新语句。我的目标是通过从具有相同A_NUMBER,C_CD和PERSONID的行中复制EQ_ID和ORDERID来填充任何空白的EQ_ID和ORDERID
我要求update语句扫描EST(表名)表:
你能告诉我如何以最好的方式做到这一点吗?
附件是来自EST表的数据的屏幕截图
答案 0 :(得分:3)
您可以使用update
和相关子查询来执行此操作。 Oracle允许您从同一子查询中设置多个列:
update est
set (eq_id, order_id) = (select est2.eq_id, est2.order_id
from est est2
where est2.a_number = est.a_number and
est2.c_cd = est.c_cd and
est2.personid = est.personid and
rownum = 1
)
where eq_id = 0 and orderid = 0;
您在文字中说“空白”但数据显示为0
。可能需要修改外部where
子句以适合您的实际数据。
答案 1 :(得分:1)
Psuedo代码:
Select distinct A_NUMBER, C_CD, PERSONID
into temp_table from TableName
where EQ_ID = 0 and Order_ID = 0
-- Choose correct data types
declare @Current_ANumber varchar(100)
declare @Current_C_CD varchar(100)
declare @Current_PERSONID varchar(100)
declare @Current_EQ_ID varchar(100)
declare @Current_Order_ID varchar(100)
select @Current_ANumber = A_NUMBER, @Current_C_CD= C_CD, @Current_PERSONID= PERSONID from temp_table
while (@Current_ANumber is not null)
begin
select distinct @Current_EQ_ID = EQ_ID, @Current_Order_ID = Order_ID where EQ_ID > 0 and Order_ID > 0
update table_name set EQ_ID = @Current_EQ_ID, Order_ID = @Current_Order_ID
where A_NUMBER = @Current_ANumber
and C_CD = @Current_C_CD
and PERSONID = @Current_PERSONID
and EQ_ID = 0
and Order_ID = 0
delete from temp_table
where A_NUMBER = @Current_ANumber
and C_CD = @Current_C_CD
and PERSONID = @Current_PERSONID
set @Current_ANumber = null
set @Current_C_CD = null
set @Current_PERSONID= null
select @Current_ANumber = A_NUMBER, @Current_C_CD= C_CD, @Current_PERSONID= PERSONID from temp_table
end
我使用SQL Server,因此虽然这段代码可能不适用于Oracle,但它应该让您走上正确的道路。