Call a sequence through a function in PostgreSql - postgresql

I have a sequence in the database, and I wanted to call the sequence through a function.
I have tried below, need an assistance to create the function in a standard process, as this is not working.
select nextval('code_seq')
CREATE FUNCTION code_sequence(integer) RETURNS integer
LANGUAGE SQL
IMMUTABLE
return select nextval('code_seq');

CREATE FUNCTION local_accounts_sequence(integer) RETURNS integer
LANGUAGE SQL
IMMUTABLE
RETURN (select nextval('code_seq'));
select local_accounts_sequence(1)
check out this solution

I am able to achieve using below statements,
CREATE FUNCTION code_sequence(out seq int)
LANGUAGE plpgsql
as $$
begin
select nextval('code_seq') into seq;
end;$$

try this :
CREATE FUNCTION local_accounts_sequence() RETURNS integer
AS 'SELECT nextval(''code_seq'');'
LANGUAGE SQL;
call it like this :
select local_accounts_sequence();

Related

How to return diferent table data based on an ID passed to an SQL function

Based on this question I would like to know if it is possible to return different table data based on an ID passed to the function.
Something like (pseudocode):
CREATE FUNCTION schemaB.testFunc(p_id INT, select_param INT)
RETURNS setof schemaZ.Table_1
AS
$$
CASE
WHEN select_param = 1 THEN SELECT * FROM schemaZ.Table_1 WHERE id = p_id
WHEN select_param = 2 THEN SELECT * FROM schemaZ.Table_2 WHERE id = p_id
END;
$$
language sql;
Table_1 and Table_2 share no same columns and that invalidates the above RETURNS clause.
This is generally impossible with SQL functions. Even with a polymorphic return type, the actual return type must be determined at call time. But all statements in an SQL function are planned before the function is executed. So you'd always end up with an error message for one of the SELECT statements returning data that doesn't fit the return type.
The same can be done with dynamic SQL in a PL/pgSQL function - with some trickery:
CREATE OR REPLACE FUNCTION f_demo(_tabletype anyelement, _id int)
RETURNS SETOF anyelement LANGUAGE plpgsql AS
$func$
BEGIN
RETURN QUERY EXECUTE
format('SELECT * FROM %s WHERE id = $1', pg_typeof(_tabletype))
USING _id;
END
$func$;
Call (important!):
SELECT * FROM f_demo(null::schemaZ.Table_1, 1);
The "trick" is to cast a null value to the desired table type, thereby defining the return type and choosing from which table to select. Detailed explanation:
Refactor a PL/pgSQL function to return the output of various SELECT queries
Take this as proof of concept. Typically, there are better (safer, less confusing, more performant) solutions ...
Related:
Difference between language sql and language plpgsql in PostgreSQL functions

What is the datatype required for the IN keyword

I'm trying to create a function in PostgreSQL that uses the keyword IN. For example:
SELECT * FROM test WHERE test.value IN (1,2,23,5,123 ...etc)
I would like to make the part after the IN a function argument. However I don't know what the data type is for this. I've looked at the data type in the docs, but couldn't figure it out.
Complete example:
create function testing(test_value ???) returns SETOF test
stable
language sql
as
$$
SELECT * FROM test WHERE test.value IN test_value
$$;
alter function testing(???) owner to postgres;
You can pass an array and use the ANY operator (the condition IN (...) is being converted to = ANY(array[...])` by the optimizer anyway).
create function testing(test_values int[]) returns SETOF test
stable
language sql
as
$$
SELECT * FROM test WHERE test.value = any(test_values)
$$;
Then use it like this:
select *
from testing(array[1,2,3,4]);

how to set a function parameter like a list

I created a function like the following one,
DROP FUNCTION if exists test_list_parameter();
CREATE or REPLACE FUNCTION test_list_parameter(???)
returns setof test
as
$$
select * from test
where test.id in ($1);
$$
language sql
how to set it for querying many ids at the same time?
like the following code,
we need to set the parameter when id more than one, which like (1,2,3,4...) in where clause.
thanks for your help.
Just pass an array of the datatype that of the id. If it's integer, pass a int[] . If it's text, use text[] etc. Also, use ANY instead of IN
CREATE or REPLACE FUNCTION test_list_parameter( idlist int [])
returns setof test
as
$$
select * from test
where test.id = ANY(idlist);
$$
language sql
Call it as
select * from test_list_parameter(ARRAY[1,3]);
DEMO

PostgreSQL subselect row as function param

I want to pass the result of a subselect to a function
Say I've got an existing function that returns boolean
function report_can_edit which takes a report row type. I want to call this from another function which is passed an id for a report
( just a silly example to illustrate what I'm trying to do )
create or replace function report_can_edit(report report) returns boolean as $$
select true; -- Imagine this does some complicated stuff
$$ language sql stable;
create or replace function task_edit(task_report_id int) returns boolean as $$
select report_can_edit((select * from report where id = task_report_id))
$$ language sql stable;
This gives
ERROR: subquery must return only one column
Do I have to switch to plpgsql and select into a decared row type first? or is there a way to do this with an sql type function?
Try:
create or replace function task_edit(task_report_id int)
returns boolean as $$
select report_can_edit((select report from report where id = task_report_id))
$$ language sql stable;

Create a function from a select that includes another function

I need to create a function that outputs the integer from another function:
select schema.next_rowid('schema', 'table');
The next_rowid() function always outputs an integer. Using PostgreSQL 9.
Have you read the documentation for how to define functions?
CREATE FUNCTION my_pretty_function() RETURNS int AS $$
SELECT schema.next_rowid('schema', 'table');
$$ LANGUAGE 'sql';