简体   繁体   中英

Function that will return the updated table in PostgreSQL

I have these two subqueries which work perfectly fine. I want these two subqueries in one function and give the result together. The function will return the UPDATED table.

This query updates the product_Details TABLE1 where start_Date matches with the current_Date and time . This will ADD offer/promotional price by matching the id with the other TABLE2 named pricing . HERE, price_and_price_Type is JSONB TYPE object.

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; 

The following query updates the product_Details TABLE1 where end_date matches with the current_Date and time . This will REMOVE offer/promotional price IF ALREADY EXISTS by matching the id with the other TABLE2 named pricing . HERE, price_and_price_Type is JSONB TYPE object.


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;


I tried to write this function which isnt working as I want to return product_Details table with the updated value. But, I'm getting errors as it wants column name which I cant provide as update isnt inserting new data, Its just an update!

The function is written as.

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;

How to write this function to fit these two subqueries in the function and where I can just call the function and get the updated table.

Thanks!

As proposed by @Laurez, adding a RETURNING clause would make the job if you had only one UPDATE in your function:

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;
$$;

But as you have two UPDATE in the same function, if you want to get the results from both, then you need to group them in a SELECT clause:

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;
$$ ;

The same function in sql language:

CREATE OR REPLACE FUNCTION PRODUCT()
RETURNS setof record LANGUAGE sql VOLATILE AS $$
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 price_And_price_Type
), 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 price_And_price_Type
) SELECT * FROM update1 UNION ALL SELECT * FROM update2 ;
$$ ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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