plpgsql

Error: query has no destination for result data while using a cursor

我怕爱的太早我们不能终老 提交于 2019-12-17 21:10:26
问题 I have a function which I have written to automate the execution of a group of functions for my project. I am taking a refcursor where I am storing my required data which I will be passing as an argument to each of my functions being called and based on the argument will get executed. I am giving my code here: CREATE OR REPLACE FUNCTION ccdb.fn_automation() RETURNS void AS $BODY$ DECLARE sec_col refcursor; cnt integer; sec_code ccdb.update_qtable%ROWTYPE; new_cnt numeric; BEGIN SELECT COUNT(*

EXECUTE…USING statement in PL/pgSQL doesn't work with record type?

随声附和 提交于 2019-12-17 20:14:14
问题 I'm trying to write a function in PL/PgSQL that have to work with a table it receives as a parameter. I use EXECUTE..INTO..USING statements within the function definition to build dynamic queries (it's the only way I know to do this) but ... I encountered a problem with RECORD data types. Let's consider the follow (extremely simplified) example. -- A table with some values. DROP TABLE IF EXISTS table1; CREATE TABLE table1 ( code INT, descr TEXT ); INSERT INTO table1 VALUES ('1','a'); INSERT

Similar UTF-8 strings for autocomplete field

醉酒当歌 提交于 2019-12-17 19:54:31
问题 Background Users can type in a name and the system should match the text, even if the either the user input or the database field contains accented (UTF-8) characters. This is using the pg_trgm module. Problem The code resembles the following: SELECT t.label FROM the_table t WHERE label % 'fil' ORDER BY similarity( t.label, 'fil' ) DESC When the user types fil , the query matches filbert but not filé powder . (Because of the accented character?) Failed Solution #1 I tried to implement an

Format specifier for integer variables in format() for EXECUTE?

梦想的初衷 提交于 2019-12-17 19:44:21
问题 CREATE OR REPLACE FUNCTION getParentLtree(parent_id bigint, tbl_name varchar) RETURNS ltree AS $BODY$ DECLARE parent_ltree ltree; BEGIN -- This works fine: -- select into parent_ltree l_tree from tbl1 where id = parent_id; EXECUTE format('select into parent_ltree l_tree from %I where id = %I', tbl_name,parent_id); RETURN parent_ltree; END; $BODY$ LANGUAGE plpgsql; There are 2 issues in above function: parent_id is integer but it is replaced with quotes? What is the correct format specifier

Postgres trigger after insert accessing NEW

一世执手 提交于 2019-12-17 18:48:19
问题 I have a pretty simple trigger: CREATE OR REPLACE FUNCTION f_log_datei() RETURNS TRIGGER AS $$ BEGIN INSERT INTO logs (aktion, tabelle, benutzer_id) VALUES(TG_OP, 'dateien', NEW.benutzer_id); END; $$ LANGUAGE 'plpgsql'; CREATE TRIGGER log_datei AFTER INSERT OR UPDATE OR DELETE ON dateien FOR EACH STATEMENT EXECUTE PROCEDURE f_log_datei(); My table logs is the following: CREATE TABLE logs( id int PRIMARY KEY DEFAULT NEXTVAL('logs_id_seq'), zeit timestamp DEFAULT now(), aktion char(6), tabelle

PL/pgSQL functions: How to return a normal table with multiple columns using an execute statement

一笑奈何 提交于 2019-12-17 17:45:11
问题 I've got this PL/pgSQL function which must return some users information. CREATE OR REPLACE FUNCTION my_function( user_id integer ) RETURNS TABLE( id integer, firstname character varying, lastname character varying ) AS $$ DECLARE ids character varying; BEGIN ids := ''; --Some code which build the ids string, not interesting for this issue RETURN QUERY EXECUTE 'SELECT users.id, users.firstname, users.lastname FROM public.users WHERE ids IN (' || ids || ')'; END; $$ LANGUAGE plpgsql; The

Cursor based records in PostgreSQL

独自空忆成欢 提交于 2019-12-17 16:26:16
问题 I'm trying to use cursors for a query that joins multiple tables. I've seen that for oracle there is a cursor based record. When I try the same for Postgres, it throws some error. How can I do the same in Postgres? CREATE OR REPLACE FUNCTION avoidable_states() RETURNS SETOF varchar AS $BODY$ DECLARE xyz CURSOR FOR select * from address ad join city ct on ad.city_id = ct.city_id; xyz_row RECORD; BEGIN open xyz; LOOP fetch xyz into xyz_row; exit when xyz_row = null; if xyz_row.city like '%hi%'

Error when setting n_distinct using a plpgsql variable

懵懂的女人 提交于 2019-12-17 14:54:54
问题 I tried to use a function to set the n_distinct value for a table. The code is as follows: create temporary table _temp ( id integer ); create function pg_temp.setdistinct(_cnt real) returns void as $$ begin alter table _temp alter column id set (n_distinct=_cnt); end $$ language plpgsql; select pg_temp.setdistinct(1000); Yet receive the following errors: ERROR: invalid value for floating point option "n_distinct": _cnt CONTEXT: SQL statement "alter table _temp alter column id set (n_distinct

Window Functions or Common Table Expressions: count previous rows within range

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 13:26:47
问题 I'd like to use a window function to determine, for each row, the total number of preceding records meeting a certain criteria. A specific example: clone=# \d test Table "pg_temp_2.test" Column | Type | Modifiers --------+-----------------------------+----------- id | bigint | date | timestamp without time zone | I'd like to know for each date the count of rows within '1 hour previous' to that date . Can I do this with a window function? Or do I need to investigate CTE's? I really want to be

Function with SQL query has no destination for result data

ε祈祈猫儿з 提交于 2019-12-17 10:44:43
问题 I am trying to create a function that returns a SELECTed resultset. When I call my postgres function like this select * from tst_dates_func() I get an error as shown below: ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function "tst_dates_func" line 3 at SQL statement ********** Error ********** ERROR: query has no destination for result data SQL state: 42601 Hint: If you want to discard the results