【问题标题】:Function that will return the updated table in PostgreSQL将在 PostgreSQL 中返回更新表的函数
【发布时间】:2022-12-20 03:04:57
【问题描述】:

我有这两个子查询,它们工作得很好。 我希望这两个子查询在一个函数中并一起给出结果。 该函数将返回 UPDATED 表。

此查询更新产品详情表 1 其中开始日期与匹配当前日期时间.这将添加优惠/促销通过匹配的价格ID与另一个 TABLE2 命名价钱.在这里,price_and_price_Type 是JSONB类型对象。

update product_Details
            SET price_and_price_Type = price_and_price_Type || 
                ( pps.details::jsonb ) 
                FROM ( 
                select id, price_Details as details 
                from pricing 
                where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
                group by id, details
                ) as pps 
                where product_Details.id = "pps".id; 

以下查询更新了产品详情TABLE1,其中 end_date 与当前日期时间.这将删除优惠/促销价格 IF ALREADY EXISTS 通过匹配ID与另一个 TABLE2 命名价钱.在这里,price_and_price_Type 是JSONB类型对象。


update product_Details
set price_And_price_Type= price_And_price_Type - pps.price_details
            FROM ( 
                select id, jsonb_object_keys(price_Details) price_details 
                from pricing 
                where to_char(end_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
                group by id, price_details
                ) as pps 
            where product_Details.id = "pps".id;


我试图编写此函数,但它不起作用,因为我想返回具有更新值的 product_Details 表。但是,我收到错误,因为它需要我无法提供的列名,因为更新没有插入新数据,它只是一个更新!

函数写成。

CREATE OR REPLACE FUNCTION PRODUCT()
RETURNS TABLE() 
AS 
$$
BEGIN
return query 
update product_Details
            SET price_and_price_Type = price_and_price_Type || 
                ( pps.details::jsonb ) 
                FROM ( 
                select id, price_Details as details 
                from pricing 
                where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
                group by id, details
                ) as pps 
                where product_Details.id = "pps".id; 
update product_Details
set price_And_price_Type= price_And_price_Type - pps.price_details
            FROM ( 
                select id, jsonb_object_keys(price_Details) price_details 
                from pricing 
                where to_char(end_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
                group by id, price_details
                ) as pps 
            where product_Details.id = "pps".id;
END;
$$
LANGUAGE PLPGSQL;

如何编写此函数以适应函数中的这两个子查询以及我可以在哪里调用函数并获取更新的表。

谢谢!

【问题讨论】:

  • 也许UPDATE ... RETURNING *。尝试通过一次更新来完成。
  • @LaurenzAlbe,RETURN TABLE ( ) 中的 () 是什么?
  • @LaurenzAlbe 如果我尝试这个 - CREATE OR REPLACE FUNCTION PRODUCT() RETURNS TABLE(id_num int, updated_price_And_price_Details jsonb) ,错误弹出,更新查询不返回元组
  • 然后让它返回元组。
  • @LaurenzAlbe 我是 postgresql 的新手,所以无法解决这个问题。你能帮忙做一下这个功能吗?

标签: json postgresql sql-update


【解决方案1】:

正如@Laurez 所建议的那样,如果您的函数中只有一个 UPDATE,添加一个 RETURNING 子句就可以完成这项工作:

CREATE OR REPLACE FUNCTION PRODUCT()
RETURNS setof record LANGUAGE PLPGSQL AS 
$$
BEGIN
return query 
update product_Details
            SET price_and_price_Type = price_and_price_Type || 
                ( pps.details::jsonb ) 
                FROM ( 
                select id, price_Details as details 
                from pricing 
                where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
                group by id, details
                ) as pps 
                where product_Details.id = "pps".id
RETURNING * ;
END;
$$;

但是由于您在同一个函数中有两个 UPDATE,如果您想从两者中获得结果,那么您需要将它们分组在一个 SELECT 子句中:

CREATE OR REPLACE FUNCTION PRODUCT()
RETURNS setof record LANGUAGE PLPGSQL AS 
$$
BEGIN
return query 
WITH update1 AS (
update product_Details
            SET price_and_price_Type = price_and_price_Type || 
                ( pps.details::jsonb ) 
                FROM ( 
                select id, price_Details as details 
                from pricing 
                where to_char(start_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
                group by id, details
                ) as pps 
                where product_Details.id = "pps".id
                returning *
), update2 AS (
update product_Details
            SET price_And_price_Type= price_And_price_Type - pps.price_details
            FROM ( 
                select id, jsonb_object_keys(price_Details) price_details 
                from pricing 
                where to_char(end_date,'YYYY-MM-DD HH24:MI') = to_char(now(),'YYYY-MM-DD HH24:MI')
                group by id, price_details
                ) as pps 
                where product_Details.id = "pps".id
                returning *
) SELECT * FROM update1 UNION ALL SELECT * FROM update2 ;
END;
$$ ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多