Postgres query function - postgresql

CREATE OR REPLACE FUNCTION public.getappointmentwithfilter(
userid integer,
periodid integer)
RETURNS TABLE(user_id integer,
scheduleid integer,
member_id varchar,
appoinmentdate timestamp,
membername character varying
)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
RETURN QUERY
SELECT
a.userid ,
a.schedule_id,
a.memberid,
a.appoinment_date,
p.name
FROM
member_appointment_details a
LEFT OUTER JOIN
patients p
ON
p.patientid=a.memberid
WHERE
a.userid=userid
AND
CASE
WHEN periodid = 1 THEN
date(a.appoinment_date) = date(now())
WHEN periodid = 2 THEN
date('week',a.appoinment_date) = date('week',CURRENT_TIMESTAMP)
WHEN periodid = 3 THEN
date_part('month',a.appoinment_date) = date_part('month',CURRENT_TIMESTAMP)
ELSE
userid=usrid::integer
END
END;
$BODY$;
I want to have case for date week month in this function. I tried like this but got error!

Related

Postgres mistakes parameter for column

I am new to Postgres DBs, and I am trying to write a function (like a stored procedure) to all inserting a user into my database. I know I must be missing something obvious, but I keep getting an error in the code shown where Postgres says { error: column "p_organizationsid" does not exist }. This is the snippet of code that appears to be causing the problem.
CREATE OR REPLACE FUNCTION userupsertget(
p_id uuid,
p_username character varying,
p_organizationid uuid,
p_organizationsid character varying,
p_lastname character varying,
p_firstname character varying,
p_middleinitial character varying,
p_emailaddress character varying,
p_sid character varying,
p_languageid integer,
p_forcepasswordreset bit,
p_salt character varying,
p_hash character varying,
p_statusid uuid,
p_createdby uuid,
p_modifiedby uuid,
p_lastsessionuguid uuid,
p_roleids uuid[],
p_applicationid uuid)
RETURNS SETOF users
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
BEGIN
DO
$do$
DECLARE establishedOrgID uuid;
BEGIN
SELECT org.id FROM organization AS org
WHERE org.sid = p_organizationsid
OR org.id = p_organizationid;
IF EXISTS (SELECT 1 FROM users WHERE id = p_id) THEN
--update statement;
UPDATE users
SET username = p_username,
lastname = p_lastname,
firstname = p_firstname,
middleinitial = p_middleinitial,
emailaddress = p_emailaddress,
sid = p_sid,
languageid = p_languageid,
forcepasswordreset = p_forcepasswordreset,
statusid = p_statusid,
datemodified = current_timestamp,
createdby = p_createdby,
modifiedby = p_modifiedby,
lastsessionguid = p_lastsessionguid
WHERE id = p_id;
IF EXISTS (SELECT 1 FROM users WHERE id = p_id AND statusid <> p_statuid) THEN
UPDATE users SET statusid = p_statusid, datestatusmodified = current_timestamp WHERE id = p_id;
END IF;
ELSE
INSERT INTO users (id, organizationid, username, lastname, firstname, middleinitial,
emailaddress, sid, languageid, forcepasswordreset, statusid, datecreated,
datemodified, createdby, modifiedby, lastsessionguid)
SELECT p_id, establishedOrgID, p_username, p_lastname, p_firstname, p_middleinitial, p_emailaddress, p_sid,
p_languageid, p_forcepasswordreset, p_statusid, current_timestamp, current_timestamp,
p_createdby, p_modifiedby, p_lastsessionuguid
FROM users
WHERE id = p_id;
-- DECLARE newid uuid := LASTVAL();
INSERT INTO users (id, userid, roleid, applicationid, startdate, enddate, createdby, modifiedBy)
SELECT LASTVAL(), roleid, p_applicationid, current_timestamp, current_timestamp, p_createdby, p_modifiedby
FROM UNNEST (p_roleids) AS roleids;
END IF;
END
$do$;
RETURN QUERY
SELECT usr.*, org.uselogin
FROM users AS usr
INNER JOIN organization AS org On org.id = usr.organizationid
WHERE id = p_id;
END;
$BODY$;
As you can see in the PostgreSQL documentation about Creating a function: https://www.postgresql.org/docs/9.1/sql-createfunction.html
you should get better luck if you replace your parameter's name by $n where n is the number of the function parameter:
CREATE FUNCTION add(integer, integer) RETURNS integer
AS 'select $1 + $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
or...
CREATE FUNCTION check_password(uname TEXT, pass TEXT)
RETURNS BOOLEAN AS $$
DECLARE passed BOOLEAN;
BEGIN
SELECT (pwd = $2) INTO passed
FROM pwds
WHERE username = $1;
RETURN passed;
END;
$$ LANGUAGE plpgsql
SECURITY DEFINER
-- Set a secure search_path: trusted schema(s), then 'pg_temp'.
SET search_path = admin, pg_temp;
Now, if you wish to continue using parameter names, you should declare them as alias:
CREATE or REPLACE FUNCTION data_ctl(opcao char, fdata date, fhora time) RETURNS char(10) AS $$
DECLARE
opcao ALIAS FOR $1;
vdata ALIAS FOR $2;
vhora ALIAS FOR $3;
retorno char(10);
BEGIN
IF opcao = 'I' THEN
insert into datas (data, hora) values (vdata, vhora);
retorno := 'INSERT';
END IF;
IF opcao = 'U' THEN
update datas set data = vdata, hora = vhora where data='1995-11-01';
retorno := 'UPDATE';
END IF;
IF opcao = 'D' THEN
delete from datas where data = vdata;
retorno := 'DELETE';
ELSE
retorno := 'NENHUMA';
END IF;
RETURN retorno;
END;
$$
LANGUAGE plpgsql;
--select data_ctl('I','1996-11-01', '08:15');
select data_ctl('U','1997-11-01','06:36');
select data_ctl('U','1997-11-01','06:36');

Postgres recursive function with return - on dbeaver

I'm trying to create function on postgresql db using dbeaver. but when trying to save function i get error. its says "syntax error at or near "RETURN"". Maybe some one will see where is the problem.
CREATE OR REPLACE FUNCTION public.concept_children_by_id(concept_id bigint)
RETURNS TABLE(concept_description character varying, component_id bigint, father_id bigint, curr_level integer)
LANGUAGE plpgsql
AS $function$
BEGIN
WITH RECURSIVE OURCTE(component_id, father_id, curren_level) AS
(
SELECT
rel."sourceId",
rel."destinationId",
1::integer
FROM
en."sct2_Relationship_Snapshot_INT" AS rel
WHERE
rel."destinationId" = concept_id
AND
rel.active = '1'
AND
rel."typeId" = 116680003
UNION ALL
SELECT
e."sourceId",
e."destinationId",
rl.curren_level + '1'
FROM
OURCTE rl,
en."sct2_Relationship_Snapshot_INT" e
WHERE
e."destinationId" = rl."component_id"
AND
e.active = '1'
AND
e."typeId" = 116680003
)
RETURN QUERY
SELECT DISTINCT
des.term,
O.component_id,
O.father_id,
O.curren_level
FROM OURCTE O
INNER JOIN en."sct2_Description_Snapshot-en_INT" des ON (O.component_id = des."conceptId")
WHERE
des."typeId" = 900000000000003001;
ORDER BY curren_level;
RETURN;
END;
$function$

How can I use join for returned result?

Now I have the following function:
CREATE OR REPLACE FUNCTION set_city(_city_id bigint, _country_id integer, _lat float, lon float) RETURNS geo_cities LANGUAGE plpgsql as $$
DECLARE
city_coords Geometry := ST_SetSrid(ST_MakePoint(_lon, _lat), 3395);
result record;
BEGIN
IF EXISTS (SELECT 1 FROM geo_cities gc WHERE gc.id = _city_id)
THEN
UPDATE geo_cities
SET coords = city_coords, country_id = _country_id
WHERE id = _city_id
RETURNING * INTO result;
ELSE
INSERT INTO geo_cities(id, country_id, coords)
VALUES (_city_id, _country_id, city_coords)
RETURNING * INTO result;
END IF;
RETURN result;
END;
$$
I want to use join with my result. That's what I mean:
...
RETURNING
id as city_id,
ST_X(coords) as lon,
INNER JOIN geo_countries as gc ON gc.id = id
...
Can I do that?
You can join the result of a function call to a table, but in a slightly different way:
SELECT
gc.col_1, gc.col_2, /* ... as many as needed */
city.id AS city_id, ST_X(city.coords) AS lon
FROM
set_city(1234, 5678, 1.23, 3.45) AS city
JOIN geo_countries AS gc ON gc.id = city.id
So, in practice, set_city(...) behaves in the SELECT just is if it were any other kind of table.

How to return setof records from function?

I'm trying to create a function which will return setof record. I want to use the function as follows:
SELECT city_name FROM set_city(1, 1, 'ExampleName');
My function:
CREATE OR REPLACE FUNCTION set_city(_city_id integer, _country_id integer, _city_name varchar)
RETURNS SETOF RECORD
LANGUAGE plpgsql
as $$
DECLARE
result record;
BEGIN
IF EXISTS (SELECT 1 FROM geo_cities gc WHERE gc.id = _city_id)
THEN
UPDATE geo_cities
SET country_id = _country_id, city_name = _city_name
WHERE id = _city_id
RETURNING * INTO result;
ELSE
INSERT INTO geo_cities(id, country_id, city_name)
VALUES (_city_id, _country_id, _city_name)
RETURNING * INTO result;
END IF;
-- It's wrong
RETURN QUERY SELECT result;
END;
$$
What should I change?
You could change the return statement:
...
-- It's wrong
-- RETURN QUERY SELECT result;
RETURN NEXT result; -- that's good
...
However, a column definition list is required for functions returning "record", so you would have to add it in every query:
SELECT city_name FROM set_city(1, 1, 'ExampleName')
AS (id int, country_id int, city_name text);
In fact the function returns a single row of the type geo_cities and you do not need setof:
DROP FUNCTION set_city(_city_id integer, _country_id integer, _city_name varchar);
CREATE OR REPLACE FUNCTION set_city(_city_id integer, _country_id integer, _city_name varchar)
RETURNS geo_cities
LANGUAGE plpgsql
as $$
DECLARE
result geo_cities;
BEGIN
IF EXISTS (SELECT 1 FROM geo_cities gc WHERE gc.id = _city_id)
THEN
UPDATE geo_cities
SET country_id = _country_id, city_name = _city_name
WHERE id = _city_id
RETURNING * INTO result;
ELSE
INSERT INTO geo_cities(id, country_id, city_name)
VALUES (_city_id, _country_id, _city_name)
RETURNING * INTO result;
END IF;
RETURN result;
END;
$$;
SELECT city_name FROM set_city(1, 1, 'ExampleName');
Note that you can get the same functionality in a single SQL statement:
INSERT INTO geo_cities(id, country_id, city_name)
VALUES (1, 1, 'ExampleName')
ON CONFLICT (id) DO UPDATE SET
country_id = excluded.country_id,
city_name = excluded.city_name
RETURNING *;

PostgreSQL using variables in FOR Loop

I have two tables:
CREATE TABLE arapply
(
arapply_id serial NOT NULL,
arapply_postdate date,
arapply_source_docnumber text,
arapply_target_docnumber text,
arapply_target_paid numeric
);
CREATE TABLE aropenbal
(
ar_id integer,
doc_number text,
doc_type text,
doc_date date,
base_amount numeric,
paid_amount numeric,
open_balance numeric
);
For each entry in aropenbal, I want to SUM arapply.arapply_target_paid values where arapply.arapply_source_docnumber = aropenbal.doc_number (if aropenbal.doctype is C or R) or arapply.arapply_target_docnumber = aropenbal.doc_number (if aropenbal.doctype is not C or R) AND also arapply_postdate <= aropenbal.doc_date. The result should be stored to aropenbal.paid_amount.
I then wish to update aropenbal.open_balance with aropenbal.base_amount + aropenbal.paid_amount.
The function should return the total (SUM) of aropenbal.open_balance.
I'm having problems with the code below. The SELECT statements inside the FOR loop don't work, unless I manually assign a value say '362' in place of r.docnumber. Otherwise, the result is zero.
Seems to be a formatting problem.
Any insights?
CREATE OR REPLACE FUNCTION testit() RETURNS NUMERIC AS
$BODY$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT * FROM aropenbal ORDER BY doc_date
LOOP
UPDATE aropenbal SET paid_amount = (
CASE WHEN (doc_type IN ('C', 'R')) THEN
(SELECT COALESCE (SUM (arapply_target_paid)* -1, 0)
FROM arapply
WHERE arapply_source_docnumber = r.doc_number
AND arapply_postdate <= r.doc_date)
ELSE
(SELECT COALESCE(SUM (arapply_target_paid),0)
FROM arapply
WHERE arapply_target_docnumber = r.doc_number
AND arapply_postdate <= r.doc_date)
END) WHERE ar_id = r.ar_id;
UPDATE aropenbal SET open_balance = (base_amount - paid_amount)
WHERE ar_id = r.ar_id;
END LOOP;
RETURN (SELECT SUM(open_balance) FROM aropenbal);
END;
$BODY$
LANGUAGE plpgsql;