简体   繁体   English

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

[英]Syntax error while creating function in postgresql

I got a syntax error while creating a procedure in postgresql.Here I attached my code.I got a error syntax error near "Continue" 我在postgresql中创建过程时遇到语法错误。这里附上了我的代码。“ Continue”附近出现错误语法错误

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

You are trying to use a MySQL (or other DB?) function in PostgreSQL. 您正在尝试在PostgreSQL中使用MySQL(或其他数据库?)函数。 There is no concept of CONTINUE HANDLER in PostgreSQL, so you have to convert the function into PostgreSQL format. 在PostgreSQL中没有CONTINUE HANDLER概念,因此您必须将函数转换为PostgreSQL格式。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM