I have a issue whether I'm facing an issue in concatenating multiple values using Cursor/ Function in postgreSQL.
I have a table names Products which contains several values of customers who buy different products,so there can be multiple entries for customers.
So my requirement is to get a part of an HTML if I give the customer email as parameter.
For example: If I give [email protected] which contains two entries in the table the output should be like shown below,
<p style="line-height: 14px; text-
align: center; font-size: 12px; margin: 0;"> product 1 </p>
<p style="line-height: 14px; text-
align: center; font-size: 12px; margin: 0;"> product 2 </p>
But now I'm getting the details of one product only,like this, product 1
CREATE OR REPLACE FUNCTION
Append_Products(Customer_Email TEXT)
RETURNS text AS $$
DECLARE
rowcount BIGINT;
Products TEXT DEFAULT '';
HTMLscript TEXT DEFAULT '<p style="line-height: 14px; text-
align: center; font-size: 12px; margin: 0;">';
rec_Product RECORD;
cur_AppendProducts CURSOR(Customer_Email TEXT)
FOR SELECT "Name", "Product","itemcount"
FROM dl."Products"
WHERE "email" = Customer_Email;
BEGIN
-- Open the cursor
OPEN cur_Appendproducts(Customer_Email);
LOOP
-- fetch row into the film
FETCH cur_Appendproducts INTO rec_Product;
-- exit when no more row to fetch
EXIT WHEN NOT FOUND;
-- build the output
IF rec_Product.itemcount > 0 THEN
Products := HTMLscript || rec_Product."Product" || '</p>';
END IF;
END LOOP;
-- Close the cursor
CLOSE cur_Appendproducts;
RETURN Products;
END; $$ LANGUAGE plpgsql;
FORloop. It looks like you really want toRETURN SETOF textalso.