在postgresql中创建函数时出现语法错误

时间:2015-07-23 10:41:22

标签: postgresql

我在postgresql中创建过程时出现语法错误。我附上了我的代码。我在"继续"

附近出现错误语法错误
create function patient_form_values() RETURNS void AS
 $$ begin

DECLARE columnName varchar(200) ;
DECLARE done boolean default true;
DECLARE CONTINUE handler for not found set done = false;
DECLARE cur1 cursor for select distinct COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'currentdiagnosis';

open cur1;
read_loop : loop
fetch from cur1 into columnName;
if done then leave read_loop; 
end if;

set @insertValues := concat('INSERT INTO patient_form_temp(patient_id, form_template_id, creator_id, created_date)
SELECT c.patient_id as patient_id, 41 AS form_template_id, 2 AS creator_id, c.created_date AS created_date 
FROM currentdiagnosis c 
WHERE c.', columnName,' IS NOT NULL GROUP BY c.patient_id, c.created_date'); 
select @insertValues;
prepare stmt from @insertValues;
execute stmt;

end loop;
close cur1;
end ;

$$ LANGUAGE plpgsql

2 个答案:

答案 0 :(得分:1)

您正在尝试在PostgreSQL中使用MySQL(或其他数据库?)函数。 PostgreSQL中没有CONTINUE HANDLER的概念,因此您必须将该函数转换为PostgreSQL格式。

答案 1 :(得分:0)

drop FUNCTION if exists migratePartnerAdvertiser();
CREATE OR REPLACE FUNCTION migratePartnerAdvertiser() RETURNS int4 AS '

DECLARE r RECORD;

BEGIN
    FOR r IN select distinct COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ''currentdiagnosis'' and table_schema=''public'' LOOP

          EXECUTE concat(''INSERT INTO patient_form_temp(patient_id, form_template_id, creator_id, created_date) SELECT c.patient_id as patient_id, 41 AS form_template_id, 2 AS creator_id, c.reg_date AS created_date FROM currentdiagnosis c WHERE c.'' , r.column_name , '' IS NOT NULL GROUP BY c.patient_id, c.reg_date'');

    END LOOP;
return 1;
END;
' LANGUAGE plpgsql;