plpgsql

Run SQL statements in PL/pgSQL only if a row doesn't exist

 ̄綄美尐妖づ 提交于 2019-12-29 09:22:36
问题 I want to do something like this in a PL/pgSQL function in Postgres 9.6: INSERT INTO table1 (id, value) VALUES (1, 'a') ON CONFLICT DO NOTHING; --- If the above statement didn't insert a new row --- because id of 1 already existed, --- then run the following statements INSERT INTO table2 (table1_id, value) VALUES (1, 'a'); UPDATE table3 set (table1_id, time) = (1, now()); However, I don't know how to determine whether the first INSERT actually inserted a new row, or whether the the ON

Update a table with a trigger after update

有些话、适合烂在心里 提交于 2019-12-29 08:55:31
问题 I have two tables batch (batch_id,start_date,end_date,batch_strength,is_locked) sem (user_id,is_active,no_of_days) I have executed the trigger procedure given below then update the table using query CREATE OR REPLACE FUNCTION em_batch_update() RETURNS trigger AS $em_sem_batch$ BEGIN UPDATE batch set is_locked='TRUE' where (start_date + (select no_of_days from sem WHERE is_active='TRUE' and user_id='OSEM') ) <= current_date; return NEW; END; $em_sem_batch$ LANGUAGE plpgsql; CREATE TRIGGER em

Execute multiple functions together without losing performance

不羁的心 提交于 2019-12-29 07:31:15
问题 I have this process that has to make a series of queries, using pl/pgsql: --process: SELECT function1(); SELECT function2(); SELECT function3(); SELECT function4(); To be able to execute everything in one call, I created a process function as such: CREATE OR REPLACE FUNCTION process() RETURNS text AS $BODY$ BEGIN PERFORM function1(); PERFORM function2(); PERFORM function3(); PERFORM function4(); RETURN 'process ended'; END; $BODY$ LANGUAGE plpgsql The problem is, when I sum the time that each

PL/pgSQL anonymous code block

有些话、适合烂在心里 提交于 2019-12-29 01:37:15
问题 In PostgreSQL 9.0 I have this PLPGSQL anonymous code block: DO $$ DECLARE bigobject integer; BEGIN SELECT lo_creat(-1) INTO bigobject; ALTER LARGE OBJECT bigobject OWNER TO postgres; INSERT INTO files (id, "mountPoint", data, comment) VALUES (15, '/images/image.png', bigobject, 'image data'); SET search_path = pg_catalog; SELECT pg_catalog.lo_open(bigobject, 131072); SELECT pg_catalog.lowrite(0, '\\x000001000100101010000000000028010000160000002800000010000000200000000100040'); SELECT pg

plpgsql function returns table(..)

ぐ巨炮叔叔 提交于 2019-12-28 12:03:28
问题 I'm trying to get this plpgsql function to work: CREATE OR REPLACE FUNCTION outofdate(actualdate varchar) RETURNS TABLE(designacion varchar(255),timebeingrotten varchar(255)) AS $BODY$ SELECT designacao, actualdate - prazo FROM alimento WHERE prazo < actualdate; $BODY$ LANGUAGE 'plpgsql' volatile; SELECT * From outofdate('12/12/2012'); It keeps giving me an error on line 2 - table .. ERROR: syntax error at or near "TABLE" LINE 2: RETURNS TABLE(designacion varchar(255),timebeingrotten varch...

Select from a table variable

无人久伴 提交于 2019-12-28 04:28:06
问题 I am trying to save the result of a SELECT query, pass it, and reuse it in another PL/pgSQL function: DECLARE table_holder my_table; --the type of table_holder is my_table; result text; BEGIN SELECT * INTO table_holder FROM table_holder ; result = another_function(table_holder); return result; END The code for another_function(table_holder my_table) , respectively: BEGIN RETURN QUERY SELECT col FROM table_holder where id = 1; END Is it possible to run a SELECT query on a variable? If not, is

Execute a dynamic crosstab query

拜拜、爱过 提交于 2019-12-27 12:28:02
问题 I implemented this function in my Postgres database: http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/ Here's the function: create or replace function xtab (tablename varchar, rowc varchar, colc varchar, cellc varchar, celldatatype varchar) returns varchar language plpgsql as $$ declare dynsql1 varchar; dynsql2 varchar; columnlist varchar; begin -- 1. retrieve list of column names. dynsql1 = 'select string_agg(distinct '||colc||'||'' '|

Execute a dynamic crosstab query

拈花ヽ惹草 提交于 2019-12-27 12:27:10
问题 I implemented this function in my Postgres database: http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/ Here's the function: create or replace function xtab (tablename varchar, rowc varchar, colc varchar, cellc varchar, celldatatype varchar) returns varchar language plpgsql as $$ declare dynsql1 varchar; dynsql2 varchar; columnlist varchar; begin -- 1. retrieve list of column names. dynsql1 = 'select string_agg(distinct '||colc||'||'' '|

Issue when migrating Stored Procedure from SQL Server to PostgreSQL

戏子无情 提交于 2019-12-25 12:08:51
问题 I come from SQL Server to PostgreSQL (9.0), so I am having a issue in a stored procedure / function when executing it. The function is returning this error message: SQLSTATE: 42601; SQLERRM: query has no destination for result data What I need to do pass the values from columns selected by the SELECT query along with the OUT parameters into the result and avoid getting that error message? CREATE OR REPLACE FUNCTION myplfunction( IN i_param1 character varying, IN i_param2 character varying, IN

Postgres syntax error in a function

那年仲夏 提交于 2019-12-25 09:35:08
问题 I am trying to create a function and I can't find my error in the following code: CREATE OR REPLACE FUNCTION qwat_od.fn_label_create_fields(table_name varchar, position boolean = true, rotation boolean = true) RETURNS void AS $BODY$ BEGIN /* Creates columns */ EXECUTE 'ALTER TABLE qwat_od.'||table_name||' ADD COLUMN label_1_visible smallint default 1; '; IF position IS TRUE THEN EXECUTE 'ALTER TABLE qwat_od.'||table_name||' ADD COLUMN label_1_x double precision default null;'; EXECUTE 'ALTER