PL/SQL 如何将记录转换为关联数组
PL/SQL How to convert a record to an assoc array
我正在尝试使用光标从 table(员工)获取数据并保存到 asso。大批。然而,使用游标获取数据到记录更直接,并且将记录转换为关联数组(arr)很麻烦。下面的代码是我试图将数据提取到 assoc 数组并且需要改进的代码。还是游标以外的任何方法?谢谢。
DECLARE
TYPE AssoArray IS TABLE OF varchar2(30) INDEX BY varchar2(30);
arr AssoArray;
table_rec employee%rowtype;
CURSOR cur IS
SELECT * FROM employee;
BEGIN
OPEN cur;
LOOP
FETCH cur into table_rec;
EXIT WHEN cur%notfound;
-- how to improve the code in the section below,
arr('col1') := table_rec.col1;
arr('col2') := table_rec.col2;
arr('col3') := table_rec.col3;
...
arr('col50') := table_rec.col50;
-- end of section
-- do sth
END LOOP;
END;
你能解释一下你想要得到什么吗?但是我在你的代码中看到 arr AssoArray 将只包含一个最后获取的行并且将在每个周期被重写。你真的需要吗?我的建议是你想获得一些 table 行作为关联数组。如果它是真的,您可以将数组创建为行类型的 table 并且(例如)通过 id 对其进行索引(如果您的 ID-s 是整数)。
TYPE AssoArray IS TABLE OF employee%rowtype INDEX BY PLS_INTEGER;
例如
create table tmp_table_01 as select * from all_objects where rownum < 11;
DECLARE
TYPE AssoArray IS TABLE OF tmp_table_01%rowtype INDEX BY varchar2(30);
arr AssoArray;
table_rec tmp_table_01%rowtype;
CURSOR cur IS
SELECT * FROM all_objects where rownum < 20;
BEGIN
OPEN cur;
LOOP
FETCH cur into table_rec;
EXIT WHEN cur%notfound;
-- how to improve the code in the section below,
arr(table_rec.object_name) := table_rec;
-- end of section
dbms_output.put_line(table_rec.object_name ||' '||arr(table_rec.object_name).object_id );
-- do sth
END LOOP;
END;
编辑:
如果您想通过 table 结构进行比较,您可以使用动态 SQL。 NExt 代码从 table TMP_TABLE_01 中获取数据,按 object_id 排序,比较相邻行和 return 差异行计数。
DECLARE
l_sql varchar2(32767);
TYPE t_cols_type IS table of varchar2(30);
l_cols t_cols_type ;
l_result number;
BEGIN
SELECT c.COLUMN_NAME
BULK COLLECt INTO l_cols
FROM user_tab_cols c
WHERE c.TABLE_NAME = 'TMP_TABLE_01';
l_sql := 'WITH s AS (SELECT t.*, row_number() OVER (ORDER BY object_id) AS rn FROM TMP_TABLE_01 t)
SELECT count(*)
FROM s
JOIN s sub_s ON s.rn = sub_s.rn - 1
where 1=0 ';
FOR i IN 1 .. l_cols.last LOOP
l_sql := l_sql || ' OR decode(s.'||l_cols(i)||',sub_s.'||l_cols(i)||',1,0) = 0';
END LOOP;
dbms_output.put_line(l_sql);
EXECUTE IMMEDIATE l_sql
INTO l_result ;
dbms_output.put_line('count of conseuence different rows ' ||l_result);
END;
我正在尝试使用光标从 table(员工)获取数据并保存到 asso。大批。然而,使用游标获取数据到记录更直接,并且将记录转换为关联数组(arr)很麻烦。下面的代码是我试图将数据提取到 assoc 数组并且需要改进的代码。还是游标以外的任何方法?谢谢。
DECLARE
TYPE AssoArray IS TABLE OF varchar2(30) INDEX BY varchar2(30);
arr AssoArray;
table_rec employee%rowtype;
CURSOR cur IS
SELECT * FROM employee;
BEGIN
OPEN cur;
LOOP
FETCH cur into table_rec;
EXIT WHEN cur%notfound;
-- how to improve the code in the section below,
arr('col1') := table_rec.col1;
arr('col2') := table_rec.col2;
arr('col3') := table_rec.col3;
...
arr('col50') := table_rec.col50;
-- end of section
-- do sth
END LOOP;
END;
你能解释一下你想要得到什么吗?但是我在你的代码中看到 arr AssoArray 将只包含一个最后获取的行并且将在每个周期被重写。你真的需要吗?我的建议是你想获得一些 table 行作为关联数组。如果它是真的,您可以将数组创建为行类型的 table 并且(例如)通过 id 对其进行索引(如果您的 ID-s 是整数)。
TYPE AssoArray IS TABLE OF employee%rowtype INDEX BY PLS_INTEGER;
例如
create table tmp_table_01 as select * from all_objects where rownum < 11;
DECLARE
TYPE AssoArray IS TABLE OF tmp_table_01%rowtype INDEX BY varchar2(30);
arr AssoArray;
table_rec tmp_table_01%rowtype;
CURSOR cur IS
SELECT * FROM all_objects where rownum < 20;
BEGIN
OPEN cur;
LOOP
FETCH cur into table_rec;
EXIT WHEN cur%notfound;
-- how to improve the code in the section below,
arr(table_rec.object_name) := table_rec;
-- end of section
dbms_output.put_line(table_rec.object_name ||' '||arr(table_rec.object_name).object_id );
-- do sth
END LOOP;
END;
编辑: 如果您想通过 table 结构进行比较,您可以使用动态 SQL。 NExt 代码从 table TMP_TABLE_01 中获取数据,按 object_id 排序,比较相邻行和 return 差异行计数。
DECLARE
l_sql varchar2(32767);
TYPE t_cols_type IS table of varchar2(30);
l_cols t_cols_type ;
l_result number;
BEGIN
SELECT c.COLUMN_NAME
BULK COLLECt INTO l_cols
FROM user_tab_cols c
WHERE c.TABLE_NAME = 'TMP_TABLE_01';
l_sql := 'WITH s AS (SELECT t.*, row_number() OVER (ORDER BY object_id) AS rn FROM TMP_TABLE_01 t)
SELECT count(*)
FROM s
JOIN s sub_s ON s.rn = sub_s.rn - 1
where 1=0 ';
FOR i IN 1 .. l_cols.last LOOP
l_sql := l_sql || ' OR decode(s.'||l_cols(i)||',sub_s.'||l_cols(i)||',1,0) = 0';
END LOOP;
dbms_output.put_line(l_sql);
EXECUTE IMMEDIATE l_sql
INTO l_result ;
dbms_output.put_line('count of conseuence different rows ' ||l_result);
END;