[英]How to loop through columns and retrieve column values?
我对Oracle游标有疑问。
我需要比较选定列的列表中的值,查看更新后哪些记录已更改。 该表非常大,区分新记录和旧记录的唯一方法是使用Batch_ID。
输出表将包含以下信息:
所以我有一个非常基本的东西,如下所示,除了我需要写几百条SELECT语句才能浏览几百列。
SELECT a.*, b.LAST_name AS last_name_updated --- here the variable could be ethnicity, income category etc..
from (SELECT person_id, last_name
FROM person
WHERE batch_id = (select max(batch_id) from person)
) a FULL OUTER JOIN
(SELECT person_id, last_name
FROM person
WHERE batch_id = (select max(batch_id) - 1 from person)
) b
ON a.person_id = b.person_id
WHERE nvl(a.last_name,0) <> nvl(b.last_nm,0)
我需要浏览大约500个字段名称的列表,例如收入,种族,姓氏等,并捕获所有这些列中的所有更改(如果有)。
我如何在不编写数百条SELECT
语句的情况下进行此操作?
在此先感谢您的帮助!
这是一条通用的SQL语句,它将使用ALL_TAB_COLS生成可用于比较列的SQL语句:
SELECT CASE WHEN column_id = '1' THEN q'[ select 'batchid: ' || batchid ]' END
|| CASE
WHEN column_name <> 'BATCHID'
THEN
q'[ || case when ]'
|| column_name
|| q'[<> lag(]'
|| column_name
|| q'[) over (partition by batchid order by col1, col2) then ' ]'
|| column_name
|| ' has changed'' end'
END
|| CASE WHEN column_id = MAX (column_id) OVER () THEN q'[ as changemessage from testtable]' END
sql
FROM all_tab_cols a
WHERE table_name = 'TESTTABLE'
ORDER BY column_id;
测试用例:
CREATE TABLE testtable
(
batchid INTEGER
, col1 INTEGER
, col2 INTEGER
);
BEGIN
FOR batchid IN 1 .. 3
LOOP
FOR col1 IN 1 .. 3
LOOP
FOR col2 IN 1 .. 2
LOOP
INSERT INTO testtable
VALUES (batchid, col1, col2);
END LOOP;
END LOOP;
END LOOP;
END;
执行SQL,然后生成以下SQL语句:
select 'batchid: ' || batchid
|| case when COL1<> lag(COL1) over (partition by batchid order by col1, col2) then ' COL1 has changed' end
|| case when COL2<> lag(COL2) over (partition by batchid order by col1, col2) then ' COL2 has changed' end as changemessage from testtable
我将它留给您以减少结果集:
CHANGEMESSAGE
batchid: 1
batchid: 1
batchid: 1
batchid: 1
batchid: 1 COL2 has changed
batchid: 1
batchid: 1
batchid: 1
batchid: 1 COL1 has changed COL2 has changed
batchid: 1
batchid: 1
batchid: 1
batchid: 1 COL2 has changed
batchid: 1
batchid: 1
batchid: 1
batchid: 1 COL1 has changed COL2 has changed
batchid: 1
batchid: 1
batchid: 1
batchid: 1 COL2 has changed
batchid: 1
batchid: 1
batchid: 1
batchid: 2
batchid: 2
batchid: 2
batchid: 2
batchid: 2 COL2 has changed
batchid: 2
batchid: 2
batchid: 2
batchid: 2 COL1 has changed COL2 has changed
batchid: 2
batchid: 2
batchid: 2
batchid: 2 COL2 has changed
batchid: 2
batchid: 2
batchid: 2
batchid: 2 COL1 has changed COL2 has changed
batchid: 2
batchid: 2
batchid: 2
batchid: 2 COL2 has changed
batchid: 2
batchid: 2
batchid: 2
batchid: 3
batchid: 3
batchid: 3
batchid: 3
batchid: 3 COL2 has changed
batchid: 3
batchid: 3
batchid: 3
batchid: 3 COL1 has changed COL2 has changed
batchid: 3
batchid: 3
batchid: 3
batchid: 3 COL2 has changed
batchid: 3
batchid: 3
batchid: 3
batchid: 3 COL1 has changed COL2 has changed
batchid: 3
batchid: 3
batchid: 3
batchid: 3 COL2 has changed
batchid: 3
batchid: 3
batchid: 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.