我可以从Postgresql函数内部创建临时表吗?
这是Postgresql脚本。我想把它变成一个函数我可以从Postgresql函数内部创建临时表吗?,postgresql,Postgresql,这是Postgresql脚本。我想把它变成一个函数 CREATE TEMPORARY TABLE bad_survey ( survey_id int8 NOT NULL, template_id int8 NOT NULL ); analyze bad_survey; insert into bad_survey(survey_id, template_id) (select id as survey_id, template_id from survey_storage where st
CREATE TEMPORARY TABLE bad_survey (
survey_id int8 NOT NULL,
template_id int8 NOT NULL
);
analyze bad_survey;
insert into bad_survey(survey_id, template_id)
(select id as survey_id, template_id
from survey_storage
where status in ('Never Surveyed','Incomplete Configuration','Ready to Launch')
and id=original_row_id
and tenant_id=owner_tenant_id
and tenant_id=5);
insert into bad_survey(survey_id, template_id)
(select pss.id, pss.template_id
from survey_storage css
inner join company_by_path cbp
on css.company_by_path_id = cbp.id
and css.tenant_id = cbp.tenant_id
and cbp.relationship_type = 'partner'
inner join survey_storage pss
on cbp.owner_tenant_id = pss.tenant_id
and css.master_template_id = pss.master_template_id
and css.tenant_id = pss.owner_tenant_id
and css.source_id = pss.source_id
and css.tenant_id != pss.tenant_id
and css.template_id != pss.template_id
and pss.id != pss.original_row_id
where css.id in (select id as survey_id
from survey_storage
where status in ('Never Surveyed','Incomplete Configuration','Ready to Launch')
and id=original_row_id
and tenant_id=owner_tenant_id
and tenant_id=5));
DELETE FROM survey_user su
USING bad_survey bs
WHERE su.survey_id = bs.survey_id;
DELETE FROM survey_library_users slu
USING bad_survey bs
WHERE slu.survey_library_id = bs.template_id;
DELETE FROM row_history rh
USING bad_survey bs
WHERE rh.row_id = bs.survey_id;
DELETE FROM survey_storage ss
USING bad_survey bs
WHERE ss.id = bs.survey_id;
DELETE FROM survey_library sl
USING bad_survey bs
WHERE sl.id = bs.template_id;
在这里,我看到我在两个地方硬编码了租户id。我想把它作为函数参数传递。
但当我试图将此代码包装到存储过程中时,它会在创建临时表时出现语法错误
更新
以下是失败的函数创建脚本:
CREATE OR REPLACE FUNCTION public.purge_bad_surveys(_tenant_id bigint)
RETURNS bool
LANGUAGE plpgsql
AS $function$
BEGIN
CREATE TEMPORARY TABLE bad_survey (
survey_id int8 NOT NULL,
template_id int8 NOT NULL
);
analyze bad_survey;
insert into bad_survey(survey_id, template_id)
(select id as survey_id, template_id
from survey_storage
where status in ('Never Surveyed','Incomplete Configuration','Ready to Launch')
and id=original_row_id
and tenant_id=owner_tenant_id
and tenant_id=_tenant_id);
insert into bad_survey(survey_id, template_id)
(select pss.id, pss.template_id
from survey_storage css
inner join company_by_path cbp
on css.company_by_path_id = cbp.id
and css.tenant_id = cbp.tenant_id
and cbp.relationship_type = 'partner'
inner join survey_storage pss
on cbp.owner_tenant_id = pss.tenant_id
and css.master_template_id = pss.master_template_id
and css.tenant_id = pss.owner_tenant_id
and css.source_id = pss.source_id
and css.tenant_id != pss.tenant_id
and css.template_id != pss.template_id
and pss.id != pss.original_row_id
where css.id in (select id as survey_id
from survey_storage
where status in ('Never Surveyed','Incomplete Configuration','Ready to Launch')
and id=original_row_id
and tenant_id=owner_tenant_id
and tenant_id=_tenant_id));
DELETE FROM survey_user su
USING bad_survey bs
WHERE su.survey_id = bs.survey_id;
RAISE NOTICE 'Done with deleting survey users';
DELETE FROM survey_library_users slu
USING bad_survey bs
WHERE slu.survey_library_id = bs.template_id;
RAISE NOTICE 'Done with deleting survey_library_users';
DELETE FROM row_history rh
USING bad_survey bs
WHERE rh.row_id = bs.survey_id;
RAISE NOTICE 'Done with deleting row_history';
DELETE FROM survey_storage ss
USING bad_survey bs
WHERE ss.id = bs.survey_id
RAISE NOTICE 'Done with deleting survey_storage';
DELETE FROM survey_library sl
USING bad_survey bs
WHERE sl.id = bs.template_id;
RAISE NOTICE 'Done with deleting survey_library';
return true;
end;
$function$
下面是我尝试持久化函数时的错误消息:
SQL Error [42601]: ERROR: syntax error at or near "RAISE"
Position: 1893
ERROR: syntax error at or near "RAISE"
Position: 1893
ERROR: syntax error at or near "RAISE"
Position: 1893
另一次更新
在注释掉所有的raisenotice之后,我能够继续使用该函数。现在,为什么“提高通知”不起作用?您在报告行之前的语句中缺少分号
从测量存储中删除
使用bad_调查bs
其中ss.id=bs.survey_id;——向我们显示给您一个错误的代码。@Kamil,我已经更新了问题以显示给您一个错误的代码。很抱歉,没有用更正更新问题,我发现当我注释掉“提出通知”时“行&它仍然没有编译。但是在分号更正之后,它仍然在发出通知时失败。然而,我被告知我的代码是正确的,只有我执行代码的ide DBeaver不能处理raise通知或任何其他raise语句。我们将直接从shell在服务器中运行代码,看看是否有问题。