打开游标以根据 oracle 中相同 table 的其他列值获取和更新 table 的列值
Opening cursor to fetch and update column values of a table based on other column values of same table in oracle
我必须根据以下逻辑更新 table 中的最后 2 列,它有 42 列。
- C42 的字段值(在下面的等式中替代 N)= ("First column position with a null/no value" - 1)
- C41 的字段值 = C(N) 列的字段值,其中 N =(“C42”列的值减 1)
注意:Table 值的设置方式是,当在列中遇到第一个空值时;在任何特定的 record.Table 中,后面的其他列肯定是空的,它有大约 100K 条记录,它是一个中间 table,其中每周重复上述计算,并且在 table 和最后一个中填充新值每周计算两列。
例如:
C42 = C19 ( 20 - 1)
C41 = C(20) when 20 columns have some value and 21st column is null
尝试创建存储过程并打开游标以获取计算值,但在创建逻辑和根据逻辑更新每一行时遇到问题。有人可以建议一种有效的方法来执行上述计算逻辑并更新每条记录。提前致谢
如果我理解你在做什么,你不需要 PL/SQL,你只需要使用合并和 case 表达式进行简单更新 - 诚然,两者都有很多术语,所以有点笨拙。
使用大大简化的 table,只需担心四列,加上您要更新的第 41 和第 42 列:
create table your_table (c1 number, c2 number, c3 number, c4 number, c41 number, c42 number);
insert into your_table (c1, c2) values (11, 12);
insert into your_table (c1, c2, c3) values (23, 24, 25);
您可以通过以相反顺序合并所有其他列来获得 c42
值:
coalesce(c4, c3, c2, c1)
或者您的情况:
coalesce(c40, c39, c38, c37, ..., c4, c3, c2, c1)
您可以使用如下 case 表达式获取该列的位置:
case
when c40 is not null then 40
when c39 is not null then 39
...
when c4 is not null then 4
when c3 is not null then 3
when c2 is not null then 2
when c1 is not null then 1
end;
您可以查询只是为了查看值(使用我简化的 table):
select c1, c2, c3, c4,
coalesce(c4, c3, c2, c1) as c41,
case
when c4 is not null then 4
when c3 is not null then 3
when c2 is not null then 2
when c1 is not null then 1
end as c42
from your_table;
C1 C2 C3 C4 C41 C42
---------- ---------- ---------- ---------- ---------- ----------
11 12 12 2
23 24 25 25 3
您只需更新:
update your_table
set c41 = coalesce(c4, c3, c2, c1),
c42 =
case
when c4 is not null then 4
when c3 is not null then 3
when c2 is not null then 2
when c1 is not null then 1
end;
2 rows updated.
select * from your_table;
C1 C2 C3 C4 C41 C42
---------- ---------- ---------- ---------- ---------- ----------
11 12 12 2
23 24 25 25 3
如果这是您要经常做的事情,那么您可以创建这些虚拟列,这样它们会自动计算并始终保持最新。
我必须根据以下逻辑更新 table 中的最后 2 列,它有 42 列。
- C42 的字段值(在下面的等式中替代 N)= ("First column position with a null/no value" - 1)
- C41 的字段值 = C(N) 列的字段值,其中 N =(“C42”列的值减 1)
注意:Table 值的设置方式是,当在列中遇到第一个空值时;在任何特定的 record.Table 中,后面的其他列肯定是空的,它有大约 100K 条记录,它是一个中间 table,其中每周重复上述计算,并且在 table 和最后一个中填充新值每周计算两列。
例如:
C42 = C19 ( 20 - 1)
C41 = C(20) when 20 columns have some value and 21st column is null
尝试创建存储过程并打开游标以获取计算值,但在创建逻辑和根据逻辑更新每一行时遇到问题。有人可以建议一种有效的方法来执行上述计算逻辑并更新每条记录。提前致谢
如果我理解你在做什么,你不需要 PL/SQL,你只需要使用合并和 case 表达式进行简单更新 - 诚然,两者都有很多术语,所以有点笨拙。
使用大大简化的 table,只需担心四列,加上您要更新的第 41 和第 42 列:
create table your_table (c1 number, c2 number, c3 number, c4 number, c41 number, c42 number);
insert into your_table (c1, c2) values (11, 12);
insert into your_table (c1, c2, c3) values (23, 24, 25);
您可以通过以相反顺序合并所有其他列来获得 c42
值:
coalesce(c4, c3, c2, c1)
或者您的情况:
coalesce(c40, c39, c38, c37, ..., c4, c3, c2, c1)
您可以使用如下 case 表达式获取该列的位置:
case
when c40 is not null then 40
when c39 is not null then 39
...
when c4 is not null then 4
when c3 is not null then 3
when c2 is not null then 2
when c1 is not null then 1
end;
您可以查询只是为了查看值(使用我简化的 table):
select c1, c2, c3, c4,
coalesce(c4, c3, c2, c1) as c41,
case
when c4 is not null then 4
when c3 is not null then 3
when c2 is not null then 2
when c1 is not null then 1
end as c42
from your_table;
C1 C2 C3 C4 C41 C42
---------- ---------- ---------- ---------- ---------- ----------
11 12 12 2
23 24 25 25 3
您只需更新:
update your_table
set c41 = coalesce(c4, c3, c2, c1),
c42 =
case
when c4 is not null then 4
when c3 is not null then 3
when c2 is not null then 2
when c1 is not null then 1
end;
2 rows updated.
select * from your_table;
C1 C2 C3 C4 C41 C42
---------- ---------- ---------- ---------- ---------- ----------
11 12 12 2
23 24 25 25 3
如果这是您要经常做的事情,那么您可以创建这些虚拟列,这样它们会自动计算并始终保持最新。