Cannot create datawindow with postgresql stored procedure in PowerBuilder - postgresql

I'm currently running PowerBuilder 2021 Build 1311 and PostgreSQL 13.8. I can't create a new datawindow with postgresql stored procedure in PowerBuilder.
I go to File --> New--> Select any datawindow --> Stored procedure
--> select Procedure and click Next --> Ok, The Following Error Occurs.
Cannot Create DataWindow.
SQLSTATE = 42883.
Error : sp_employee is a procedure. Error while executing a query.
Procedure code:(Simple Code)
CREATE OR REPLACE PROCEDURE public.sp_employee(
)
LANGUAGE 'plpgsql'
AS $BODY$
begin
Select * from employee;
end
$BODY$;
The stored procedure works fine in pgadmin, but it doesn't work in PowerBuilder.
Does powerbuilder support postgresql stored procedure for a source of a datawindow?

Related

rails + psql with structure dump uses PROCEDURE over FUNCTION

Every time I dump my structure.sql on a rails app, I get PROCEDURE over FUNCTION. FUNCTION is our default and I have to commit the file in parts which is annoying and sometimes I miss lines which is even worse, as it is a rather big structure.sql file.
git diff example:
-CREATE TRIGGER cache_comments_count AFTER INSERT OR DELETE OR UPDATE ON public.comments FOR EACH ROW EXECUTE PROCEDURE public.update_comments_counter();
+CREATE TRIGGER cache_comments_count AFTER INSERT OR DELETE OR UPDATE ON public.comments FOR EACH ROW EXECUTE FUNCTION public.update_comments_counter();
I'm sure there is a postgresql setting for this somewhere, but I can't find it.
Whether you use Function or Procedure you get exactly the same. The documentation shows
CREATE [ CONSTRAINT ] TRIGGER name...
EXECUTE { FUNCTION | PROCEDURE } function_name ( arguments )
This means you can use either term FUNCTION or PROCEDURE but either way function_name is always called. See demo. For demo I have separate triggers for insert and update. Insert using execute procedure and update using execute function. This cannot be changed in Postgres it would have to be Rails setting. NOTE: Prior to v11 Postgres only allowed execute procedure even though you had to create a trigger function that was called.
The function pg_get_triggerdef() changed between Postgres 11 and 12 when Postgres introduced real procedures. Since Postgres 12 it always returns a syntax that uses EXECUTE FUNCTION as in reality it is a function that is called when the trigger fires, not a procedure.
So this code:
create table t1 (id int);
create function trg_func()
returns trigger
as
$$
begin
return new;
end;
$$
language plpgsql;
create trigger test_trigger
before insert or update
on t1
for each row
execute procedure trg_func();
select pg_get_triggerdef(oid)
from pg_trigger
where tgname = 'test_trigger';
returns the following in Postgres 11 and earlier:
CREATE TRIGGER test_trigger BEFORE INSERT OR UPDATE ON public.t1 FOR EACH ROW EXECUTE PROCEDURE trg_func()
and the following in Postgres 12 and later:
CREATE TRIGGER test_trigger BEFORE INSERT OR UPDATE ON public.t1 FOR EACH ROW EXECUTE FUNCTION trg_func()
I guess Rails uses pg_get_triggerdef() to obtain the trigger source. So there is nothing you can do. If you want a consistent result, you should use the same Postgres version everywhere.
The column action_statement in the view information_schema.triggers also reflects the change in naming.
Postgres 11 example
Postgres 12 example

How to insert data from stored procedure into a new table using postgresql

I have created a stored procedure using PGADMIN4. Inside the SP, I have selected a view table. However, I want the data inside the stored procedure to be inserted into a new table. I have tried below code, but it is showing an error:
SP name: Test
New table name: Customer
Insert into public.Customer exec public.Test
This is my SP code:
create procedure test()
language plpgsql
as $$
BEGIN
Select * from public.customer_list;
END;
$$;
ERROR: syntax error at or near "exec"
Procedures cannot be used in SQL. If you must have a stored procedure (define as code stored in the database to be executed) then change to a SQL function. Which returns a type.
create function copy_customer_list()
returns setof customer_list
language sql
as $$
Select * from customer_list;
$$;
Then you can insert into other table with
insert into customer
select * from copy_customer_list();

Creating and executing a stored procedure in spring boot

I have a stored procedure written in a sql file which I want to execute through spring boot code using jdbc template. The stored procedure is like :
CREATE OR REPLACE FUNCTION DELETE_REDUNDANT_RECORDS()
RETURNS void AS
$func$
DECLARE
interval_time BIGINT DEFAULT 0;
min_time BIGINT DEFAULT 0;
max_time BIGINT DEFAULT 0;
rec_old RECORD;
rec_new RECORD;
rec_start RECORD;
v_filename VARCHAR(250);
v_systemuid VARCHAR(50);
is_continous_record BOOLEAN default false;
cursor_file CURSOR FOR
select distinct filename,systemuid from ASP.MONITORING_BOOKMARK_ORIGINAL;
cursor_data CURSOR FOR
select * from ASP.MONITORING_BOOKMARK_ORIGINAL where filename = v_filename and systemuid=v_systemuid order by mindatetime, maxdatetime;
BEGIN
--open the file cursor to fetch the filename and systemuid
OPEN cursor_file;
-- logic for procedure
CLOSE cursor_file;
END;
$func$
LANGUAGE plpgsql;
I have added this in a sql file in my spring boot project. I want to create a scheduler to create and execute this stored procedure using spring jdbc. The database used is Postgres. Is there a way available to do this. I have got references for calling a procedure but what I need is to create and execute the procedure.
For running the procedure, Spring's SimpleJdbcCall could be what you are looking for.
Its use should be fairly straightforward:
create an instance supplying a DataSource
call withCatalogName and withProcedureName as needed
call declareParameters as needed
call execute to run the procedure
For creating it, a JdbcTemplate and its own execute method should be sufficient.
Be aware that you might want to create the function once and only run it afterwards. Creating and running the procedure both as part of some scheduled operation is a quite uncommon requirement. You might want to re-assess the need to create the procedure each and every time if possible.

How can I use PL/pgSQL over a JDBC connection?

I am writing a database using flyway and maven to apply migration scripts to a Postgres 11 database.
I'd like to be able to write statements in PL/pgSQL that flyway can run over its JDBC connection, just like regular ansi SQL.
Eg,
--some regular sql statements
create table test(id bigint);
select * from test;
--etc
--switch to plpgsql
language plpgsql;
--some plpgsql code goes here
Is this possible?
You can embed PL/pgSQL in SQL using a DO statement:
DO $$
DECLARE
...
BEGIN
...
END;
$$;

Creating procedures in phpPgadmin

I want create a procedure (for copying the values of one table(mover_location) to another table(mover_history) on updation) ,and want to call up on that on a trigger in postgresql with phppgadmin.
Here is what i have tried :
PROCEDURE :-
CREATE OR REPLACE FUNCTION log_mover_location()
RETURNS trigger AS
BEGIN
INSERT INTO mover_history(reg_id,last_seen_lat,last_seen_long,last_seen_location_geog,last_updated_at)
VALUES(SELECT
mover_location.reg_id,mover_location.last_seen_lat,mover_location.last_seen_long,mover_location.last_seen_location_geog,mover_location.last_updated_at FROM mover_location) WHERE mover_history.reg_id =
#mover_location.reg_id;
END;
TRIGGER :-
CREATE TRIGGER update_mover_history
AFTER UPDATE
ON mover_location
FOR EACH ROW
EXECUTE PROCEDURE log_mover_location();
Trigger created successfully ,
But it give me a error as follows when executing the procedure:
ERROR: syntax error at or near "BEGIN"
LINE 4: BEGIN
^
the Procedure that you're created is incorrect, for example
CREATE OR REPLACE FUNCTION procedure_name()
RETURNS trigger AS
$BODY$
BEGIN
/*----logic----*/
END
$BODY$
LANGUAGE plpgsql