I come from SQL Server to PostgreSQL (9.0), so I am having a issue in a stored procedure / function when executing it. The function is returning this error message:
SQLSTATE: 42601;
SQLERRM: query has no destination for result data
What I need to do pass the values from columns selected by the SELECT
query along with the OUT
parameters into the result and avoid getting that error message?
CREATE OR REPLACE FUNCTION myplfunction(
IN i_param1 character varying,
IN i_param2 character varying,
IN i_param3 character varying,
IN i_param4 character varying,
OUT o_call_status integer,
OUT o_call_message character varying)
RETURNS record AS
$BODY$
DECLARE
val1 varchar;
val2 varchar;
...
IF (v_solution_id IS NULL) THEN
val1 := column1 FROM tbl2 WHERE column2= i_param1;
IF(val1 IS NULL) THEN
o_call_status := 1005;
o_call_message := column1 is not configured or invalid';
RETURN;
END IF;
SELECT 'mycolumnname1' as paramName,mycolumn1 as value FROM tb1 WHERE column1 = val
UNION ALL
SELECT 'mycolumnname2' as paramName,,mycolumn2 as value FROM tb1 WHERE column1 = val
UNION ALL
SELECT 'mycolumnname2' as paramName,,mycolumn2 as value FROM tb2 WHERE column2 = val2
WHERE tb2paramName4=i_val3;
o_call_status := 0;
o_call_message := '';
EXCEPTION WHEN OTHERS THEN
o_call_message := SQLERRM;
o_call_status := SQLSTATE;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
Generally, a PostgreSQL function only returns a single value (or row), or a set of values (or rows), but not both in parallel.
Return a table
You can either return the set or rows directly from the function, making it a "set-returning function", a.k.a. "table function". Then you have no other choice than to return the parameters
o_call_status
ando_call_message
with every row (if you, in fact, need them at all?!)The special variable
SQLERRM
is of typetext
, btw. Notinteger
.Or open a cursor
Or you would have to open a cursor to decouple the returned table from those two parameters.
However, the cursor only exists within the same transaction. So you need to fetch the values before you
COMMIT
orROLLBACK
.You can even hand in arbitrary cursor names. This and other details in the manual.
And there is more information about errors available since Postgres 9.3..