I have a function
drop function ProcessReward();
CREATE OR REPLACE FUNCTION ProcessReward()
RETURNS text AS $$
DECLARE
sessionid NO SCROLL CURSOR FOR SELECT pg."Setting",pg."UserId",pg."Id" FROM "Development"."PersonGame" pg inner join "Development"."Game" g on g."Id" = pg."GameId" pg."GameId"=1 for read only;
titles TEXT DEFAULT '';
rec record;
jsonrec record;
jsonrecord record;
BEGIN
OPEN sessionid;
loop
FETCH sessionid INTO rec;
if not found then
exit ;
end if;
EXECUTE 'select * from "Development"."GameRecipient" where "PersonGameId"=$1' into jsonrecord using rec."Id";
--I want to loop here every row returned by above query
--loop start
-- do your task
--loop end
end loop;
return titles;
END;
$$ LANGUAGE plpgsql;
My query
EXECUTE 'select * from "Development"."GameRecipient" where
"PersonGameId"=$1' into jsonrecord using rec."Id";
returns
col1 col2 col3
123 324 444
345 222 765
I want to process all rows returned by above query,How to achieve this in PostgreSQL.
Related
How can we replace the plsql code with count to postgresql
FOR I IN 1..variable.count
Loop
Cr:= varray (i)
End loop;
Replacement for count in postgresql
This can be executed like a normal query :
$$ declares a string works same as '' ,example: SELECT $$string$$; or SELECT $string$ 'text' $string$;
DO indicate the next string will be executed.
DO
$$
DECLARE
rRecord RECORD;
tText TEXT ;
BEGIN
FOR rRecord IN SELECT COUNT(*) FROM generate_series(1 , 10 , 1 )
LOOP
tText := rRecord.count;
RAISE NOTICE 'Notice %',tText ;
END LOOP ;
END;
$$
LANGUAGE plpgsql ;
i have created procedure, inside used cursor to update the some data, while calling the procedure it's getting the error.
create or REPLACE PROCEDURE bal_upd(p_id int) as
$$
DECLARE rc record;
----- cursor
bal_upd1 CURSOR (p_id int)
for
select * from tbal where custid = p_id;
begin
open bal_upd1 (p_id);
loop
FETCH bal_upd1 into rc;
exit when not found;
update t_trans set balance = balance + rc.trans;
COMMIT;
end loop;
close bal_upd1;
end;
$$ LANGUAGE plpgsql;
call bal_upd(1)
ERROR: cursor "bal_upd1" does not exist
CONTEXT: PL/pgSQL function bal_upd(integer) line 12 at FETCH
SQL state: 34000
create or REPLACE PROCEDURE bal_upd(p_id int) as
$$
DECLARE rc record;
----- cursor
bal_upd1 CURSOR (p_id int)
for
select * from tbal where custid = p_id;
begin
open bal_upd1 (p_id);
loop
FETCH bal_upd1 into rc;
exit when not found;
update t_trans set balance = balance + rc.trans;
COMMIT;
end loop;
close bal_upd1;
end;
$$ LANGUAGE plpgsql;
call bal_upd(1)
ERROR: cursor "bal_upd1" does not exist
CONTEXT: PL/pgSQL function bal_upd(integer) line 12 at FETCH
SQL state: 34000
You don't need a function or a loop for that:
UPDATE t_trans
SET balance = t_trans.balance + t.trans
FROM (SELECT sum(trans) AS trans
FROM tbal
GROUP BY custid) AS t
WHERE t_trans.custid = t.custid;
I tried, failed. I just found just use for loop (implicit cursor) is far more simple.
BEGIN;
CREATE temp TABLE tbal (
custid bigint
, trans numeric
);
INSERT INTO tbal VALUES (1 , 1);
INSERT INTO tbal VALUES (1 , 2);
CREATE temp TABLE t_trans (
custid bigint
, balance numeric
);
INSERT INTO t_trans VALUES (1 , 10);
COMMIT;
CREATE OR REPLACE PROCEDURE bal_upd (bigint)
AS $func$
DECLARE
rc record;
BEGIN
FOR rc IN
SELECT
*
FROM
tbal
WHERE
custid = $1 LOOP
RAISE NOTICE 'custid: %, trans: % ' , rc.custid , rc.trans;
UPDATE
t_trans ta
SET
balance = balance + (rc.trans)
WHERE
ta.custid = (rc.custid);
END LOOP;
END;
$func$
LANGUAGE plpgsql;
Then call it. CALL bal_upd(1);
I have a syntax error on a procedure (PostgreSQL) and I can't find the mistake. I need a procedure with the variable declaration, a for loop, and a transaction.
Here is a sample of the structure I'm using.
CREATE OR REPLACE PROCEDURE repor_dados()
LANGUAGE plpgsql AS
$$
DECLARE
utilizador int;
data_acesso date;
r record;
BEGIN
BEGIN -- begin transaction
select 2 ,4 into data_acesso,utilizador;
IF EXISTS(select 2) then -- condition
BEGIN
-- for each select result
FOR r IN SELECT 1,2,3
LOOP
-- do something
IF r = 1 THEN
SELECT 3;
ELSE
SELECT 4;
END IF;
END LOOP;
END;
COMMIT; -- commit transaction
END
$$
I am working with PostgreSQL 13.2.
The error is:
ERROR: syntax error at end of input LINE 30: $$
Using PostgreSQL, column values from a table for 1st record are stored in a record variable. for example: let the variable be:
recordvar
and different columns be:
name,id,gender,age
recordvar.name
But I want to retrieve that colum value like
var =name
then
recordvar.var
How can I do this? I don't want to use hstore.
Here is the sample code
CREATE OR REPLACE FUNCTION public.get_all_rooms()
RETURNS text AS
$BODY$
DECLARE
total integer;
titles TEXT DEFAULT '';
rec_croom RECORD;
rec_delement integer;
rec_locations integer;
rec_dname varchar;
starts_with text = NULL;
ends_with text = NULL;
de_name varchar;
--de_array integer[];
de_arr_val integer;
sql text;
cur_croom CURSOR
FOR SELECT *
FROM public.tmp_all_controlroom_2;
BEGIN
-- Open the cursor
OPEN cur_croom;
LOOP
-- fetch row into the film
FETCH cur_croom INTO rec_croom;
-- exit when no more row to fetch
EXIT WHEN NOT FOUND;
SELECT locationid INTO rec_locations FROM location WHERE locationcode = rec_croom.location_code;
FOR de_arr_val in select deid from dsmtbl WHERE dsid=74
LOOP
select name INTO de_name from tab1 where deid=de_arr_val;
INSERT INTO datavalue (a, b, c, d, e, value)
VALUES(de_arr_val, 1, rec_croom.cday , rec_locations, 3154, rec_room.de_name);
END LOOP;
END LOOP;
-- Close the cursor
CLOSE cur_croom;
RETURN titles;
END; $BODY$
rec_room.de_name is not working
I got a syntax error while creating a procedure in postgresql.Here I attached my code.I got a error syntax error near "Continue"
create function patient_form_values() RETURNS void AS
$$ begin
DECLARE columnName varchar(200) ;
DECLARE done boolean default true;
DECLARE CONTINUE handler for not found set done = false;
DECLARE cur1 cursor for select distinct COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'currentdiagnosis';
open cur1;
read_loop : loop
fetch from cur1 into columnName;
if done then leave read_loop;
end if;
set #insertValues := concat('INSERT INTO patient_form_temp(patient_id, form_template_id, creator_id, created_date)
SELECT c.patient_id as patient_id, 41 AS form_template_id, 2 AS creator_id, c.created_date AS created_date
FROM currentdiagnosis c
WHERE c.', columnName,' IS NOT NULL GROUP BY c.patient_id, c.created_date');
select #insertValues;
prepare stmt from #insertValues;
execute stmt;
end loop;
close cur1;
end ;
$$ LANGUAGE plpgsql
You are trying to use a MySQL (or other DB?) function in PostgreSQL. There is no concept of CONTINUE HANDLER in PostgreSQL, so you have to convert the function into PostgreSQL format.
drop FUNCTION if exists migratePartnerAdvertiser();
CREATE OR REPLACE FUNCTION migratePartnerAdvertiser() RETURNS int4 AS '
DECLARE r RECORD;
BEGIN
FOR r IN select distinct COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ''currentdiagnosis'' and table_schema=''public'' LOOP
EXECUTE concat(''INSERT INTO patient_form_temp(patient_id, form_template_id, creator_id, created_date) SELECT c.patient_id as patient_id, 41 AS form_template_id, 2 AS creator_id, c.reg_date AS created_date FROM currentdiagnosis c WHERE c.'' , r.column_name , '' IS NOT NULL GROUP BY c.patient_id, c.reg_date'');
END LOOP;
return 1;
END;
' LANGUAGE plpgsql;