plpgsql

How to get individual columns from table returned from a function?

不打扰是莪最后的温柔 提交于 2019-12-02 07:35:55
问题 I'm trying to make a function with returns table. CREATE FUNCTION karta_pacjenta(pe VARCHAR(11)) RETURNS TABLE('data' DATE,'imie' TEXT, 'nazwisko' TEXT, 'diagnoza' TEXT,'przepisany lek' TEXT) AS' BEGIN RETURN QUERY SELECT w.dzien AS dzien,p.imie, p.nazwisko, ch.nazwa, l.nazwa FROM pacjenci p, diagnozy d, choroby ch, wizyty w, leki l, recepty r WHERE p.pesel=d.pesel AND d.kod_choroby=ch.kod_choroby AND p.pesel=pe AND w.pesel=pe AND l.kod_leku=r.kod_leku AND r.nr_wizyty=w.nr_wizyty; END; '

PLPGSQL Function to Calculate Bearing

爱⌒轻易说出口 提交于 2019-12-02 07:01:14
Basically I can't get my head around the syntax of plpgsql and would appreciate some help with the following efforts. I have a table containing 1000's of wgs84 points. The following SQL will retrieve a set of points within a bounding box on this table: SELECT id, ST_X(wgs_geom), ST_Y(wgs_geom), ST_Z(wgs_geom) FROM points_table INNER JOIN (SELECT ST_Transform(ST_GeomFromText('POLYGON((-1.73576102027 1.5059743629, -1.73591122397 51.5061067655,-1.73548743495 51.5062838333,-1.73533186682 1.5061514313,-1.73576102027 51.5059743629))', 4326, 27700) ) AS bgeom ) AS t2 ON ST_Within(local_geom, t2.bgeom

Pass multiple sets or arrays of values to a function

主宰稳场 提交于 2019-12-02 06:42:08
问题 I'm writing a PL/pgSQL function in PostgreSQL 9.3.10 to return who has attended certain classes/sessions from the following table: Attendance +-------+---------+---------+ | Class | Section | Name | +-------+---------+---------+ | 1 | 1 | Amy | | 1 | 1 | Bill | | 1 | 2 | Charlie | | 1 | 2 | Dan | | 2 | 1 | Emily | | 2 | 1 | Fred | | 2 | 2 | George | +-------+---------+---------+ What I want to do is, given a array of class/section id pairs ( int[][] ), return all people who are in those

ERROR: input parameters after one with a default value must also have defaults in Postgres

一曲冷凌霜 提交于 2019-12-02 06:28:58
I am trying to set default value to a variable within the function in parameter list but getting an error: ERROR: input parameters after one with a default value must also have defaults Example: Create or replace function test(name varchar default null , city varchar default null , phonenumber varchar(20) default null , out sno bigint, address varchar) returns void as $$ Declare phonenumber AS VarChar(20); Begin phonenumber : =phonenumber; SELECT sno = MAX(ssno)+1 FROM emp; IF(sno IS NULL) then sno=IDENT_CURRENT('emp')+1; end; raise info '%',name; raise info '%',city; raise info '%'

Pass multiple sets or arrays of values to a function

Deadly 提交于 2019-12-02 06:18:07
I'm writing a PL/pgSQL function in PostgreSQL 9.3.10 to return who has attended certain classes/sessions from the following table: Attendance +-------+---------+---------+ | Class | Section | Name | +-------+---------+---------+ | 1 | 1 | Amy | | 1 | 1 | Bill | | 1 | 2 | Charlie | | 1 | 2 | Dan | | 2 | 1 | Emily | | 2 | 1 | Fred | | 2 | 2 | George | +-------+---------+---------+ What I want to do is, given a array of class/section id pairs ( int[][] ), return all people who are in those classes/sections. For example my_func(ARRAY[[1,1],[2,2]]) returns: +-------+---------+---------+ | Class |

How to get individual columns from table returned from a function?

巧了我就是萌 提交于 2019-12-02 05:11:45
I'm trying to make a function with returns table. CREATE FUNCTION karta_pacjenta(pe VARCHAR(11)) RETURNS TABLE('data' DATE,'imie' TEXT, 'nazwisko' TEXT, 'diagnoza' TEXT,'przepisany lek' TEXT) AS' BEGIN RETURN QUERY SELECT w.dzien AS dzien,p.imie, p.nazwisko, ch.nazwa, l.nazwa FROM pacjenci p, diagnozy d, choroby ch, wizyty w, leki l, recepty r WHERE p.pesel=d.pesel AND d.kod_choroby=ch.kod_choroby AND p.pesel=pe AND w.pesel=pe AND l.kod_leku=r.kod_leku AND r.nr_wizyty=w.nr_wizyty; END; ' LANGUAGE 'plpgsql'; It works quite nice, but I need one more thing. As result of this function I get

ERROR: input parameters after one with a default value must also have defaults in Postgres

社会主义新天地 提交于 2019-12-02 05:04:25
问题 I am trying to set default value to a variable within the function in parameter list but getting an error: ERROR: input parameters after one with a default value must also have defaults Example: Create or replace function test(name varchar default null , city varchar default null , phonenumber varchar(20) default null , out sno bigint, address varchar) returns void as $$ Declare phonenumber AS VarChar(20); Begin phonenumber : =phonenumber; SELECT sno = MAX(ssno)+1 FROM emp; IF(sno IS NULL)

Syntax error in dynamic SQL in pl/pgsql function

╄→гoц情女王★ 提交于 2019-12-02 04:33:56
I am using pl/pgsql in PostgreSQL 10, to create complex queries. I am testing a query with a couple of JOIN s and AND s. This is what I have so far: DROP FUNCTION IF EXISTS search_person(name text); CREATE FUNCTION search_person(name text) RETURNS TABLE(address_id integer, address_geom text, event_name text) AS $$ --DECLARE BEGIN RETURN QUERY EXECUTE 'SELECT address.id, event.name, address.geom FROM event JOIN person JOIN address JOIN person_address JOIN event_person WHERE person_address.event_id = event.id AND event_person.event_id = event.id AND person.id = event_person.person_id AND person

How to get the value of a dynamically generated field name in PL/pgSQL

冷暖自知 提交于 2019-12-02 04:31:28
Sample code trimmed down the the bare essentials to demonstrate question: CREATE OR REPLACE FUNCTION mytest4() RETURNS TEXT AS $$ DECLARE wc_row wc_files%ROWTYPE; fieldName TEXT; BEGIN SELECT * INTO wc_row FROM wc_files WHERE "fileNumber" = 17117; -- RETURN wc_row."fileTitle"; -- This works. I get the contents of the field. fieldName := 'fileTitle'; -- RETURN format('wc_row.%I',fieldName); -- This returns 'wc_row."fileTitle"' -- but I need the value of it instead. RETURN EXECUTE format('wc_row.%I',fieldName); -- This gives a syntax error. END; $$ LANGUAGE plpgsql; How can I get the value of a

Constructing a string out of several records with 2 columns

半世苍凉 提交于 2019-12-02 04:27:14
问题 I have prepared a simple SQL Fiddle for my question - In a word game written in Pl/pgSQL for PostgreSQL 10.2 player moves are stored in the table: CREATE TABLE words_scores ( mid bigint NOT NULL REFERENCES words_moves ON DELETE CASCADE, gid integer NOT NULL REFERENCES words_games ON DELETE CASCADE, uid integer NOT NULL REFERENCES words_users ON DELETE CASCADE, word text NOT NULL CHECK(word ~ '^[A-Z]{2,}$'), score integer NOT NULL CHECK(score >= 0) ); Here it is filled with some test data: