sql - 在 PL/pgSQL 中使用更新
问题描述
首先,我是 SQL 和 PostgreSQL 的新手。这可能是一个愚蠢的初学者的错误。
create or replace function temporary_function_for_getting(admission_number_text text,organization_id bigint,user_object_json json)
returns table(admission_number text ,status text)
LANGUAGE plpgsql
AS $function$
declare
select_user_with_id text :=
'select $1 as admin,
case
when $2 is null then ''Invalid organization ID''
when $1 is null then ''Invalid admission number''
when exists (update hsg_id_master set hsg_suffix=''something new'' where admission_number=$1 and organization_id=$2 returning hsg_suffix )
then ''success''
else ''User does not exists.''
end;';
begin
return query
execute select_user_with_id using admission_number_text,organization_id;
end;
$function$ ;
^这不起作用给我一个错误
SQL 错误 [42601]:错误:“更新”处或附近的语法错误
其中:PL/pgSQL 函数temporary_function_for_getting(text,bigint,json) 第 13 行,位于 RETURN QUERY。
更新查询本身工作正常,不确定我做错了什么。如果有人能指出我的资源和/或执行相同过程的更好方法,将不胜感激。
解决方案
您似乎想要基本查询的更新 CTE。尝试这样的事情:
with u as (
update hsg_id_master
set hsg_suffix = ''something new''
where admission_number = $1 and organization_id = $2
returning hsg_suffix
)
select $1 as admin,
(case when $2 is null then ''Invalid organization ID''
when $1 is null then ''Invalid admission number''
when exists (select 1 from u)
then ''success''
else ''User does not exists.''
end);
请注意,如果or是,update
则不会更新任何行,因为该子句将评估为-- 过滤掉所有行。$1
$2
NULL
where
NULL
推荐阅读
- php - Wordpress 自定义插件错误 - 警告:未定义的数组键“home_team”
- python - Imageai 检测器 RuntimeWarning
- scala - 斯卡拉:地图(f)和地图(_.f)
- python - 有没有办法根据小数位在python中对数组/列表的值进行动态分组?
- javascript - 如何双刷单滑
- ios - Xcode 12.5.1 - 无法附加到 pid:“####” - 丢失连接
- azure - 连接到 AZURE IOT 集线器时出现 SSL 错误
- angular - 使用 SignalR (.Net Core) 时如何忽略来自角度客户端的 SSL 验证错误?
- magento - Magento 2 目录图像颜色在缓存目录中更改
- python-3.x - 如何在 Python 中保存图形