I am trying to migrate Oracle to Postgres.
Wherever in Oracle we have bulk collect function, I have created plain SQL function:
like
CREATE OR REPLACE FUNCTION get_pub_access_tax(a_person_id mobile_user.id%TYPE)
RETURNS TABLE (tax_id integer) AS
$$
WITH v_pub_tax AS (SELECT tax_id FROM mobile_pub_tax)
select tax_id from v_pub_tax ;
$$ LANGUAGE sql;
But there is a scenario in Oracle where data are fetched on conditional basis: (see below example)
FUNCTION get_st_tax(
a_st_type IN VARCHAR2)
RETURN mmt_array
IS
v_tax_list mmt_array := mmt_array();
BEGIN
IF a_st_type = 'top-stories' THEN
SELECT t.tax_id BULK COLLECT INTO v_tax_list
FROM mobile_st_tax t;
RETURN v_tax_list;
ELSE
SELECT t.tax_id BULK COLLECT INTO v_tax_list
FROM mobile_st_tax t WHERE t.stream_type=a_stream_type;
RETURN v_tax_list;
END IF;
END;
How can we convert above Oracle code to Postgres?
I tried using if condition but getting syntax error(below code).
Below code I have tried executing in Postgres, which gives me syntax error.
CREATE OR REPLACE FUNCTION get_st_tax ( a_st_type IN VARCHAR)
RETURNS TABLE (tax_id integer) AS
$$
IF a_stream_type = 'top-stories' THEN
WITH v_tax_list AS (
SELECT t.tax_id FROM mobile_st_tax t)
SELECT tax_id FROM v_tax_list;
ELSE
WITH v_tax_list AS (
SELECT t.tax_id FROM mobile_st_tax t WHERE t.stream_type=a_stream_type)
SELECT tax_id FROM v_tax_list;
END IF;
$$ LANGUAGE SQL;
Kindly help.
The second function can be done in plain sql:
create function get_st_tax(a_st_type text)
returns int[] as $$
select array_agg(t.tax_id)
from mobile_st_tax t
where
t.stream_type = a_stream_type and a_st_type <> 'top_stories'
or
a_st_type = 'top-stories'
;
$$ language sql;
Related
I want to have an array of distinct integer values across my postgres table as the return value of a stored function.
The stored function currently looks like this
create or replace function get_unique_entries(id int)
returns table ("entry_id" int)
language plpgsql
as
$$
begin
return query
select distinct table.entry_id
from my_table
where x = id;
end;
$$;
When executing
select get_unique_entries(2);, I get the following error message:
structure of query does not match function result type
I tried different return types, but nothing worked for me.
Thanks in advance!
Hmm, can you give us a more complete picture of your scenario? I tried using your code and it seems to work (except I needed to replace table with my_table):
postgres=# create table my_table(x int, entry_id int, name text);
CREATE TABLE
postgres=# insert into my_table values(generate_series(1,100),generate_series(1,10),'foo');
INSERT 0 100
postgres=# create or replace function get_unique_entries(id int)
postgres-# returns table ("entry_id" int)
postgres-# language plpgsql
postgres-# as
postgres-# $$
postgres$# begin
postgres$# return query
postgres$# select distinct table.entry_id
postgres$# from my_table
postgres$# where x = id;
postgres$# end;
postgres$# $$;
ERROR: syntax error at or near "table"
LINE 8: select distinct table.entry_id
^
postgres=# create or replace function get_unique_entries(id int)
returns table ("entry_id" int)
language plpgsql
as
$$
begin
return query
select distinct my_table.entry_id
from my_table
where x = id;
end;
$$;
CREATE FUNCTION
postgres=# select get_unique_entries(2);
get_unique_entries
--------------------
2
(1 row)
postgres=#
While preparing the complete example I actually found it out myself.
As I am working with supabase, they display the datatype BIGINT as int8. I was trying to set this as return type. Setting the return type to BIGINT instead worked.
So in general check I would recommend myself and to others to check your column data types exactly.
The working example looks like this (as indicated by #richyen)
create or replace function get_unique_categories_for_platform(platformId int)
returns table ("category_fk" bigint)
language plpgsql
as
$$
begin
return query select distinct course.category_fk
from course
where platform_fk = platformId;
end;
$$;
I have a query
select count(distinct id), usertype from mytable group by usertype;
which I want to wrap in a function where usertype is a parameter
create or replace function myfun(facet text)
RETURNS TABLE(total_accounts bigint, facet text)
LANGUAGE plpgsql
AS $$
select count(distinct id), facet from mytable group by facet;
$$;
select * from myfun('usertype');
so that output column name also depends on the parameter I send.
But it shows an error 'parameter facet is used more than once'.
how do I make the output identical to the original query when I pass 'usertype' as a parameter?
You can use a dynamic query in a plpgsql function :
create or replace function myfun(OUT total_accounts bigint, INOUT facet text)
RETURNS setof record LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY EXECUTE '
select count(distinct id),' || quote_ident(facet) || ' from mytable group by ' || quote_ident(facet) ;
END ;
$$;
select * from myfun('usertype');
test result in dbfiddle
How do i create a Procedure that returns a set of rows from a table?
or is it even possible to return a tabular result set with procedure.
I tried adding returns setof students like you do in a function and table(id int) but it doesn't work.
SAMPLE CODE:
CREATE OR REPLACE PROCEDURE getStudents()
LANGUAGE plpgsql
AS $$
BEGIN
SELECT * FROM STUDENTS
COMMIT;
RETURN;
END;
$$;
I can call it but it says query has no destination for result data
Procedures aren't meant to return data, that's what functions are for.
You can use a plain SQL function for this, no need for PL/pgSQL:
CREATE OR REPLACE funct get_students()
returns setof student
LANGUAGE sqö
AS $$
select *
from students;
$$;
Then use it like a table:
select *
from get_students();
There is also no need for a commit.
Try to use function instead of procedure. I usually use this.
You need to create a ctype for fetching the data.
Put whatever columns you have to fetch from STUDENTS table.
Syntax is as follows:
CREATE TYPE students_data_ctype AS
(
column_1 int4,
column_2 varchar(100),
column_3 varchar(500)
)
Then create a funcction :
CREATE
OR
REPLACE
FUNCTION PUBLIC.getStudents
()
RETURNS SETOF students_data_ctype AS $BODY$ DECLARE res
students_data_ctype;
BEGIN
FOR res IN
SELECT
column_1,
column_2,
column_3
FROM
STUDENTS
LOOP RETURN NEXT res;
END LOOP;
END
; $BODY$ LANGUAGE 'plpgsql'
GO
Function call :
Select * FROM getStudents()
Taddaaa! You will get your data.
How to create a function which takes as argument integer[] parameter and executing query with IN clause with this parameter in loop.
In loop I want execute next select and result of this query I would like return.
Something like that:
CREATE OR REPLACE FUNCTION function_which_i_want(my_argument integer[]) RETURNS my_schema_and_table[] AS
$BODY$
DECLARE
result my_schema_and_table[];
BEGIN
FOR l IN SELECT * FROM table2 WHERE id in (my_argument) LOOP
SELECT * FROM my_schema_and_table;
END LOOP;
END;
...
I want to get union of each select in loop. one huge joined result.
Is this possible? Please help.
PL/pgSQL function
It could look like this:
CREATE OR REPLACE FUNCTION func1(_arr integer[])
RETURNS SETOF my_schema_and_table
LANGUAGE plpgsql AS
$func$
DECLARE
l record;
BEGIN
FOR l IN
SELECT *
FROM lookup_table
WHERE some_id = ANY(_arr)
LOOP
RETURN QUERY
SELECT *
FROM my_schema_and_table
WHERE link_id = l.link_id;
END LOOP;
END
$func$;
Assuming you actually want a SET of rows from your my_schema_and_table, not an array? To return the result of SELECT * FROM my_schema_and_table, declare the function as RETURNS SETOF my_schema_and_table
Rewrite the IN construct to = ANY(_arr). That's the way to use an array parameter directly. Logically equivalent.
Or use unnest() and join to the resulting table like #Clodoaldo demonstrates. That can be faster with long arrays.
Simplify to plain SQL function
This simple SQL function does the same:
CREATE OR REPLACE FUNCTION func2(_arr integer[])
RETURNS SETOF my_schema_and_table
LANGUAGE sql AS
$func$
SELECT t.*
FROM (SELECT unnest($1) AS some_id) x
JOIN lookup_table l USING (some_id)
JOIN my_schema_and_table t USING (link_id);
$func$
Assuming both tables have link_id.
Call:
SELECT * FROM func2('{21,31}'::int[]);
CREATE OR REPLACE FUNCTION function_which_i_want(my_argument integer[])
RETURNS my_schema_and_table[] AS
$BODY$
DECLARE
result my_schema_and_table[];
BEGIN
for l in
select t.*
from
table2 t
inner join
unnest(my_argument) m(id) on m.id = t.id
loop
SELECT * FROM my_schema_and_table;
END LOOP;
END;
I am able to do the following in SQL where an "array" of user_ids are passed into the where clause of a SQL query.
select * from users where id in (select user_id from profiles);
I would like to do the same thing but pass the "array" into a PostgreSQL (PL/pgSQL) function as shown below. How do I declare the function and work with the "array" within the function?
select * from users_function(select user_id from profiles);
CREATE OR REPLACE FUNCTION users_function(....)
RETURNS void AS
$BODY$
....
Declare an array datatype [] in the function then use the aggregate function array_agg to transform the select statement into an array.
CREATE OR REPLACE FUNCTION users_function(myints integer[])
$$
BEGIN
-- you need to find the bounds with array_lower and array_upper
FOR i in array_lower(myints, 1) .. array_upper(myints, 1) LOOP
Raise Notice '%', myints[i]::integer;
END LOOP;
END;
$$
select * from users_function(array_agg((select user_id from profiles)));
I could not get the nate c's array_agg approach as I described above. This is an option:
select * from test_array('{1,2}');
CREATE OR REPLACE FUNCTION test_array(user_ids integer[])
RETURNS void AS
$$
declare
begin
FOR i in array_lower(user_ids, 1) .. array_upper(user_ids, 1) LOOP
RAISE NOTICE '%', user_ids[i]::integer;
END LOOP;
end
$$
LANGUAGE plpgsql;