我的“postgresql nested cursors"
代码需要帮助。
我的计划是使用“外部游标”从表(Admissions
)获取数据行,并使用另一个表(EventLog_Staging)
中的条件评估每一行。在评估每一行后,我插入表格的结果(EventLog_Main
)。
问题是,我从第一个游标返回的记录中传递了一列,但我的INSERT
语句没有识别它。
- CURSOR代码阻止创建事件记录
DO
$Event_Log_Creation_Code$
DECLARE
rec_tbls RECORD;
schem_name VARCHAR(100) := 'mimiciii';
set_SrchPath text;
tx text;
tbl_name VARCHAR(50);
tbl_column VARCHAR(50);
col_datatype VARCHAR(50);
event_role VARCHAR(50);
life_cycle VARCHAR(50);
csr_outer_create_evnt_log REFCURSOR; --Cursor to get the data from the main table
csr_innner_create_evnt_log REFCURSOR; --Cursor to dissect each row of the main table
sql_eventlog_stge_rows text := 'SELECT tablename, columnname, datatype, eventrole, lifecycle
FROM EventLog_Staging
WHERE eventrole IS NOT NULL;';
sql_table_rows text := 'Select * from admissions;';
BEGIN
-- Setting the search path to the specified db schema
set_SrchPath := concat('SET search_path TO ',schem_name,';');
EXECUTE set_SrchPath;
-- Opening the outer cursor
OPEN csr_outer_create_evnt_log FOR EXECUTE sql_table_rows;
LOOP
--Fetch rows into the specified fields
FETCH csr_outer_create_evnt_log INTO rec_tbls;
EXIT WHEN NOT FOUND;
-- Create inner cursor
OPEN csr_innner_create_evnt_log FOR EXECUTE sql_eventlog_stge_rows;
-- Opening the inner Loop
LOOP
--Fetch rows into the specified fields
FETCH csr_innner_create_evnt_log INTO tbl_name, tbl_column, col_datatype, event_role, life_cycle;
EXIT WHEN NOT FOUND;
IF event_role = 'Event' THEN
tx :=concat('INSERT INTO EventLog_Main (case_id, activity, lifecycle, time_stamp)
VALUES (',rec_tbls.hadm_id,',''',tbl_column,''',''',life_cycle,''',',rec_tbls.tbl_column,');');
EXECUTE tx;
END IF;
--Closing the inner Loop
END LOOP;
--Closing the inner cursor
CLOSE csr_innner_create_evnt_log;
--Closing the outer Loop
END LOOP;
--Closing the outer cursor
CLOSE csr_outer_create_evnt_log;
END $Event_Log_Creation_Code$;
运行代码时的错误:
**********错误**********
错误:记录“rec_tbls”没有字段“tbl_column”
CONTEXT:SQL语句“SELECT concat('INSERT INTO EventLog_Main (case_id,activity,lifecycle,time_stamp) VALUES(',rec_tbls.hadm_id,',''',tbl_column,''',''',life_cycle,''',',rec_tbls.tbl_column,');')“
PL / pgSQL函数inline_code_block第47行在赋值
**********错误**********