plpgsql: calling a function with 2 OUT parameters - postgresql

I'm trying to fetch to values from a plpgsql function with 2 OUT paramenters but I have some problem.
These are the functions:
CREATE OR REPLACE FUNCTION get_test(OUT x text, OUT y text)
AS $$
BEGIN
x := 1;
y := 2;
END;
$$ LANGUAGE plpgsql;
----------------------------------------------------------------
CREATE OR REPLACE FUNCTION get_test_read()
RETURNS VOID AS $$
DECLARE
xx text;
yy text;
BEGIN
SELECT get_test() INTO xx, yy;
RAISE INFO 'x: <%>', xx;
RAISE INFO 'y: <%>', yy;
END;
$$ LANGUAGE plpgsql;
The output of the command:
select get_test_read();
INFO: x: <(1,2)
INFO: y: <>
get_test_read
So both the values go to the first parameter.
I cannot find some example on how to call a function like this.

As you have 2 OUT params, your function will return a record.
In order to get all values you should use function as the source of your data and put it into the FROM clause like this:
SELECT * FROM get_test() INTO xx, yy;

SELECT * INTO xx, yy FROM get_test();
UPDATE:
Explanation:
Modifying the second function:
CREATE OR REPLACE FUNCTION get_test_read()
RETURNS VOID AS $$
DECLARE
xx text;
yy text;
BEGIN
SELECT * INTO xx, yy FROM get_test();
RAISE INFO 'x: <%>', xx;
RAISE INFO 'y: <%>', yy;
END;
$$ LANGUAGE plpgsql;
This is similar to SELECT INTO with TABLE, where only get 2 values:
SELECT "FIELD1", "FIELD2" INTO variable1, variable2 FROM "TABLE" WHERE ...
Npgsql Basic Usage
PL/pgSQL Function Parameter Modes: IN, OUT, INOUT

Related

Postgresql howto dynamically change parameter value

Is it possible to dynamically set param of numeric() ? eg:
CREATE OR REPLACE somefunction() RETURNS numeric AS
DECLARE
f numeric;
x integer;
BEGIN
x := 2;
SELECT INTO f CAST(something AS numeric(12, x));
RETURN f;
END;
So, I don't need to use CASE inside my plpgsql function if possible :) Tried everything, but it does not work, cast expects constant. Thanks:)
Using Dynamic query:
CREATE OR REPLACE FUNCTION public.somefunction(something numeric)
RETURNS numeric
LANGUAGE plpgsql
AS $function$
DECLARE
f numeric;
x integer;
BEGIN
x := 2;
EXECUTE 'SELECT CAST($1 AS numeric(12, ' || x ||'))' INTO f USING something;
RETURN f;
END;
$function$
;
select somefunction(126.787);
somefunction
--------------
126.79
Alternate where you pass in the scale:
CREATE OR REPLACE FUNCTION public.somefunction(something numeric, scale integer)
RETURNS numeric
LANGUAGE plpgsql
AS $function$
DECLARE
f numeric;
BEGIN
EXECUTE 'SELECT CAST($1 AS numeric(12, ' || scale ||'))' INTO f USING something;
RETURN f;
END;
$function$
;
select somefunction(126.787,2);
somefunction
--------------
126.79
(1 row)
select somefunction(126.787,1);
somefunction
--------------
126.8

How can create one function from two functions in PostgreSQL?

I would like to create another function in PostgreSQL, function_c, from function_a and function_b.
function_a
CREATE OR REPLACE FUNCTION function_a(year_ INTEGER, col_b INTEGER)
RETURNS INTEGER AS
$$
DECLARE
vbl INTEGER;
BEGIN
SELECT COUNT(col_b) INTO vbl
FROM table1
WHERE date_part('year',table1.col_aa) = year_ AND table1.col_bb = col_b;
RETURN vbl;
END;
$$
LANGUAGE plpgsql;
function_b
CREATE OR REPLACE FUNCTION function_b(year_ INTEGER)
RETURNS INTEGER AS
$$
DECLARE
vbl_b integer;
BEGIN
SELECT COUNT(col_aa) INTO vble_b
FROM table1
WHERE date_part('year',table1.col_aa) = year_;
RETURN vbl_b
END;
$$
LANGUAGE plpgsql;
I want to create function c that including function a and b, returns:
year: year_ parameter of function_b
median: vbl of function_a divided vbl_b of function_b
How can create one function from two functions in PostgreSQL?
If I understood correctly below mentioned definition will work for you
CREATE OR REPLACE FUNCTION function_c(year_ INTEGER, col_b INTEGER)
RETURNS table (year1 integer, median numeric) AS
$$
DECLARE
var1 INTEGER;
var2 INTEGER;
var3 numeric;
BEGIN
var1=(select * from function_a(year_));
var2=(select * from function_b(year_, vol_b));
var3=var1/var2;
return query
select year_, var3;
END;
$$
LANGUAGE plpgsql;

postgreSQL : oracle sqlerrm equivalent of postgres

I'm new to PostgreSQL. I have experience in oracle. In oracle , to find the exact error, I use code 'dbms_output.put_line(sqlerrm)' . Here I have a postgresql function returning an integer value
CREATE OR REPLACE FUNCTION public.fn_sqltest(
p_id integer)
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
Declare
n integer;
begin
select off_id into n from office
where per_id=p_id;
return n ;
exception when others then
return -1;
end;
$BODY$;
ALTER FUNCTION public.fn_sqltest(character varying)
OWNER TO postgres;
I call this function as below
DO $$
DECLARE
ae integer;
BEGIN
ae:=fn_sqltest(10);
RAISE NOTICE 'exception: % % ', sqlstate , sqlerrm ;
RAISE NOTICE 'Return value is: % ', ae;
END $$;
and I get the error
ERROR: column "sqlstate" does not exist
How can I show the exact error message like sqlerrm in oracle.
I updated the function and wrote a code in function exception section
CREATE OR REPLACE FUNCTION public.fn_sqltest(
p_id integer)
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
Declare
n integer;
text_var1 text;
text_var2 text;
text_var3 text;
begin
select off_id into n from office
where per_id=p_id;
return n ;
exception when others then
GET STACKED DIAGNOSTICS text_var1 = MESSAGE_TEXT,
text_var2 = PG_EXCEPTION_DETAIL,
text_var3 = PG_EXCEPTION_HINT;
RAISE NOTICE 'Return value is: % % %',text_var1 , text_var2, text_var3;
return -1;
end;
$BODY$;
ALTER FUNCTION public.fn_sqltest(character varying)
OWNER TO postgres;
Another method is by changing the return type. I changed the return type to text and rewrite the exception section code as below
return sqlerrm;
Unlike PL/SQL, in PL/pgSQL SQLSTATE and SQLERRM are not defined outside an exception handler. See the documentation, section "Obtaining Information About An Error".
This also means that you can't get SQLSTATE and SQLERRM of a successful operation, unlike PL/SQL.
So, if you want to use these special variables outside an exception handler, you have to store them in variables.
I don't know how would you like to return them from the function. I can demonstrate this idea inside your function code:
CREATE OR REPLACE FUNCTION public.fn_sqltest(
p_id integer)
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
Declare
n integer;
v_sqlerrm text;
v_sqlstate text;
begin
begin
select off_id into n from office
where per_id=p_id;
return n ;
exception when others then
v_sqlerrm := sqlerrm;
v_sqlstate := sqlstate;
end;
RAISE NOTICE 'exception: % % ', v_sqlstate , v_sqlerrm ;
return -1;
end;
$BODY$;
I know this is old but just for the record: The easiest way to output the state and message of error is to raise them from within the exception clause. You don't need to declare extra variables.
CREATE OR REPLACE FUNCTION fn_sqltest(p_id integer)
RETURNS integer
AS $$
DECLARE
n integer;
BEGIN
SELECT off_id
INTO n
FROM office
WHERE per_id = p_id;
RETURN n;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'exception: % - %', SQLSTATE, SQLERRM;
RETURN -1;
END;
$$ LANGUAGE plpgsql;

postgresql: converting CTE column to array

I am trying to convert one of the columns of CTE to array. I keep on getting "syntax error at or near" followed by "ret := array(".
My objective is that the table I am returning from a_function() in example below is stored as a variable to be referred later in function. But I could not find a syntax to do so. So, instead of using CTE, if I can use something else, that would work just as nicely.
Note: I am trying this in pgAdmin III.
create or replace function a_function()
--returns int[][] as
returns table(column1 int, column2 int) as
$body$
begin
return query
select 1,2;
end;
$body$
language 'plpgsql'
;
--select * from a_function();
create or replace function test_a_function()
returns void as
$body$
declare ret int[];
begin
with ret_cte(column1, column2) as (
select * from a_function()
)
ret := array(
select column1 from ret_cte
)
;
--raise notice '%', array_to_string(ret, ',');
end;
$body$
language 'plpgsql'
;
--select test_a_function();
I am trying to convert one of the columns of CTE to array. I keep on getting "syntax error at or near" followed by "ret := array("
You could use:
ret :=array(with ret_cte(column1, column2) as (
select * from a_function()
)
select column1 from ret_cte
);
Rextester Demo
Arrays can be built and returned using the array_agg() function. For example, your second function could be written using a SQL language function that returns the array as follows:
create or replace function test_a_function()
returns int[] as
$body$
select array_agg(column1) from a_function();
$body$
language 'sql';
Alternatively, you can assign the array to a variable like this:
create or replace function test_a_function()
returns void as
$body$
declare
ret int[];
begin
select array_agg(column1)
into ret
from a_function();
raise info '%', ret;
end;
$body$
language 'plpgsql';

Create a function with an argument as a subselect

I'd like to create a function for select and changed me data
CREATE OR REPLACE FUNCTION PublicatedTask( argument ) RETURNS SETOF task AS $$DECLARE
f task%ROWTYPE;
BEGIN
FOR f IN SELECT * FROM Task where layer IN $1 and publicationin<>0 ORDER BY id LOOP
if (f.publicationIN = 1) then
f.description='';
end if;
RETURN NEXT f;
END LOOP;
RETURN;
END;
$$
LANGUAGE 'plpgsql';
but I 'dont know what argument type?
I'd like to do SELECT * FROM PublicatedTask((1,2,3));
Thanks for your help
Or use VARIADIC:
CREATE OR REPLACE FUNCTION PublicatedTask( VARIADIC argument int[]) RETURNS SETOF task AS $$DECLARE
f task%ROWTYPE;
BEGIN
FOR f IN SELECT * FROM Task where layer = ANY($1) and publicationin<>0 ORDER BY id LOOP
if (f.publicationIN = 1) then
f.description='';
end if;
RETURN NEXT f;
END LOOP;
RETURN;
END;
$$
LANGUAGE 'plpgsql';
And use it this way:
SELECT * FROM PublicatedTask(1,2,3);
VARIADIC is available as of version 8.4: http://www.postgresql.org/docs/8.4/interactive/xfunc-sql.html#XFUNC-SQL-VARIADIC-FUNCTIONS
you could use an array of integers as parameter:
CREATE OR REPLACE FUNCTION PublicatedTask( argument int[]) RETURNS SETOF task AS $$DECLARE
f task%ROWTYPE;
BEGIN
FOR f IN SELECT * FROM Task where layer = ANY($1) and publicationin<>0 ORDER BY id LOOP
if (f.publicationIN = 1) then
f.description='';
end if;
RETURN NEXT f;
END LOOP;
RETURN;
END;
$$
LANGUAGE 'plpgsql';
Then you could call it this way:
SELECT * FROM PublicatedTask('{1,2,3}');