Postgresql - Stored Procedure that returns a table result set - postgresql

Im new to PostgreSQL v.13 and want to create a Stored Procedure that will receive an integer value and then return the results from a table.
I have this:
create or replace procedure public.sp_message_template_get(
templateid int
) language plpgsql AS $$
begin
-- subtracting the amount from the sender's account
select * from public.message_template;
--commit;
end;
$$
Then I try to call it by using:
call public.sp_message_template_get(1);
And I get the following error:
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function sp_message_template_get(integer) line 6 at SQL statement
SQL state: 42601
Any clue?
Thanks

Create a sql function.
create or replace function message_template_get(templateid integer)
returns setof message_template language sql as
$$
-- subtracting the amount from the sender's account (sql query)
select * from public.message_template;
$$;
If there is a reason to use plpgsql language then use return query.
create or replace function message_template_get(templateid integer)
returns setof message_template language plpgsql as
$$
begin
-- subtracting the amount from the sender's account
return query select * from public.message_template;
-- maybe other statements here
end;
$$;
Please note that commit is not necessary and illegal in a function body. More than one return query or return next statements are possible.

Related

Error Message : Query has no destination for result data

I am new to postgreSQL. I am trying to convert SQL store procedure into postgreSQL store function. I tried Like below store function created successfully but when I am trying to execute I am getting following error message. I am not getting where I need to replace PERFORM.
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
SQL Store Procedure
CREATE PROCEDURE [dbo].[roomType]
#intInstId int
AS
BEGIN
SELECT
nClientRoomTypeID,sClientRTDesc,sClientRTName,sClientRTCode
FROM
ClientRoomType
WHERE
ClientRoomType.nInstID=#intInstId
ORDER BY
ClientRoomType.sClientRTCode
END
GO
PostgreSQl Store Function
CREATE OR REPLACE FUNCTION roomType (int_inst_id int)
RETURNS VOID
AS $$
BEGIN
SELECT
nclient_room_type_id,sclient_rt_desc,sclient_rt_name,sclient_rt_code
FROM
clientroomtype
WHERE
clientroomtype.ninst_id=int_inst_id
ORDER BY
clientroomtype.sclient_rt_code;
END;
$$ LANGUAGE plpgsql;
I assume you're attempting to return the query result with your function. You actually do not need plpgsql language for this, but in case you need it for somthing else, use this syntax:
CREATE OR REPLACE FUNCTION roomType (int_inst_id int)
RETURNS TABLE (res_nclient_room_type_id INT,res_sclient_rt_desc TEXT,res_sclient_rt_name TEXT, res_sclient_rt_code TEXT)
AS $$
BEGIN
RETURN QUERY
SELECT
nclient_room_type_id,sclient_rt_desc,sclient_rt_name,sclient_rt_code
FROM
clientroomtype
WHERE
clientroomtype.ninst_id=int_inst_id
ORDER BY
clientroomtype.sclient_rt_code;
END;
$$ LANGUAGE plpgsql;
How to use it?
SELECT * FROM roomType(1);
Since I do not have your data, I cannot test it. But it follows this principle:
CREATE OR REPLACE FUNCTION f ()
RETURNS TABLE (b boolean, x int)
AS $$
BEGIN
RETURN QUERY SELECT TRUE, 1;
END;
$$ LANGUAGE plpgsql;
SELECT * FROM f();
b | x
---+---
t | 1
(1 Zeile)

Postgresql stored procedure return table all columns

A function is created. The function has a input parameter. I can return a column but I want to return all table columns. Also I want to do if result is zero the function return just 0. How can I do it?
Here the error result.
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function dwgcould.returnallcolumns(character varying) line 3 at SQL statement
********** Error **********
ERROR: query has no destination for result data
SQL state: 42601
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Context: PL/pgSQL function dwgcould.returnallcolumns(character varying) line 3 at SQL statement
CREATE OR REPLACE FUNCTION dwgcould.returnallcolumns(IN sessionId character varying)
RETURNS SETOF public.mytable AS
$BODY$
BEGIN
SELECT * FROM public.mytable WHERE session_id=returnallcolumns.sessionId ORDER BY pro_id DESC LIMIT 1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
If you want to return a result, you need to use return query in PL/pgSQL as documented in the manual
CREATE OR REPLACE FUNCTION dwgcould.returnallcolumns(IN sessionId character varying)
RETURNS SETOF public.mytable AS
$BODY$
BEGIN
return query --<< this was missing
SELECT *
FROM public.mytable
WHERE session_id = returnallcolumns.sessionId
ORDER BY pro_id DESC LIMIT 1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
But you don't need PL/pgSQL for this, a simple SQL function will be more efficient:
CREATE OR REPLACE FUNCTION dwgcould.returnallcolumns(IN sessionId character varying)
RETURNS SETOF public.mytable AS
$BODY$
SELECT *
FROM public.mytable
WHERE session_id = returnallcolumns.sessionId
ORDER BY pro_id DESC LIMIT 1;
$BODY$
LANGUAGE sql;
How to get all data from dynamic table in the PostgreSQL database which name pass as in parameter?
DROP FUNCTION IF EXISTS schemaName."GetAllDataFromDynamicTable";
CREATE OR REPLACE FUNCTION schemaName."GetAllDataFromDynamicTable"(IN P_DynamicTableName text)
RETURNS SETOF schemaName."P_DynamicTableName"
AS $$
BEGIN
return query
SELECT *
FROM schemaName."P_DynamicTableName" -- this table name every time change
END;
$$
LANGUAGE plpgsql;

Postgres Stored procedure in select statement cannot get the datas

I am trying to get the data from the Database use function of select prison();.but i got error .Please advise me.
CREATE OR REPLACE FUNCTION prison() RETURNS refcursor AS $$
DECLARE
ref refcursor;
BEGIN
OPEN ref FOR SELECT round,ben_sc,ben_st FROM prison_issue;
RETURN ref;
END;
$$ LANGUAGE plpgsql;
and calling like this
select prison();
also i tried.but cannot executed the rows.
BEGIN;
SELECT prison();
-- Returns: <unnamed portal 2>
FETCH ALL IN "<unnamed portal 24>";
COMMIT;
There is no need for a PL/pgSQL function for this:
CREATE OR REPLACE FUNCTION prison()
RETURNS setof prison_issue
AS $$
SELECT * FROM prison_issue;
$$ LANGUAGE sql;
You also need to use:
select * from prison();
to retrieve the data, do not use select prison() (which only returns a single record, not multiple rows)
You didn't show us your definition of the table prison_issue if you don't want to return all columns you need something like this:
CREATE OR REPLACE FUNCTION prison()
RETURNS table (round integer, ben_sc text, ben_st text)
AS $$
SELECT SELECT round,ben_sc,ben_st FROM prison_issue;
$$ LANGUAGE sql;
You will need to adjust the part table (round integer, ben_sc text, ben_st text) to match the data type of the columns you select.
Below is an example code.I have assumed the data types.Replace with the real ones.
CREATE OR REPLACE FUNCTION prison() RETURNS TABLE(round numeric,ben_sc character varying,ben_st character varying) AS $$
BEGIN
RETURN QUERY SELECT p.round,p.ben_sc,p.ben_st FROM prison_issue p;
END;
$$ LANGUAGE plpgsql;

Returning from a function with OUT parameter

I have an error, but I don't know what the problem is.
I want execute a function and return a value from a column filled in by the column default, a sequence - the equivalent of currval(sequence).
I use:
PostgreSQL 9.0
pgAdmin III
CREATE OR REPLACE FUNCTION name_function(in param_1 character varying
, out param_2 bigint)
AS
$$
BEGIN
INSERT INTO table (collumn_seq,param_1) VALUES (DEFAULT,param_1)
returning collumn_seq;
--where:collumn_seq reference a collumn serial..
END;
$$
LANGUAGE plpgsql VOLATILE;
I can create the function without error but when trying to execute, the following error is returned:
SELECT name_function('GHGHGH');
ERROR: The query has no destination for result data
It would work like this:
CREATE OR REPLACE FUNCTION name_function(param_1 varchar
, OUT param_2 bigint)
LANGUAGE plpgsql AS
$func$
BEGIN
INSERT INTO table (collumn_seq, param_1) -- "param_1" also the column name?
VALUES (DEFAULT, param_1)
RETURNING collumn_seq
INTO param2;
END
$func$;
Normally, you would add a RETURN statement, but with OUT parameters, this is optional.
Refer to the manual for more details:
Returning from a function
Executing a Query with a Single-row Result
The simple case can be covered with a plain SQL function.
And you can omit the target column that shall get its DEFAULT value.
And you can just as well use a RETURNS clause in this case:
CREATE OR REPLACE FUNCTION name_function(param_1 varchar)
RETURNS bigint
LANGUAGE sql AS
$func$
INSERT INTO table (param_1) -- "param_1" also the column name?
VALUES (param_1)
RETURNING collumn_seq;
$func$;

How to use EXECUTE FORMAT ... USING in postgres function

CREATE OR REPLACE FUNCTION dummytest_insert_trigger()
RETURNS trigger AS
$BODY$
DECLARE
v_partition_name VARCHAR(32);
BEGIN
IF NEW.datetime IS NOT NULL THEN
v_partition_name := 'dummyTest';
EXECUTE format('INSERT INTO %I VALUES ($1,$2)',v_partition_name)using NEW.id,NEW.datetime;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION dummytest_insert_trigger()
OWNER TO postgres;
I'm trying to insert using
insert into dummyTest values(1,'2013-01-01 00:00:00+05:30');
But it's showing error as
ERROR: function format(unknown) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Context: PL/pgSQL function "dummytest_insert_trigger" line 8 at EXECUTE statement
I'm unable get the error.
Your function could look like this in Postgres 9.0 or later:
CREATE OR REPLACE FUNCTION dummytest_insert_trigger()
RETURNS trigger AS
$func$
DECLARE
v_partition_name text := quote_ident('dummyTest'); -- assign at declaration
BEGIN
IF NEW.datetime IS NOT NULL THEN
EXECUTE
'INSERT INTO ' || v_partition_name || ' VALUES ($1,$2)'
USING NEW.id, NEW.datetime;
END IF;
RETURN NULL; -- You sure about this?
END
$func$ LANGUAGE plpgsql;
About RETURN NULL:
To ignore result in BEFORE TRIGGER of PostgreSQL?
I would advice not to use mixed case identifiers. With format( .. %I ..) or quote_ident(), you'd get a table named "dummyTest", which you'll have to double quote for the rest of its existence. Related:
Are PostgreSQL column names case-sensitive?
Use lower case instead:
quote_ident('dummytest')
There is really no point in using dynamic SQL with EXECUTE as long as you have a static table name. But that's probably just the simplified example?
You need explicit cast to text:
EXECUTE format('INSERT INTO %I VALUES ($1,$2)'::text ,v_partition_name) using NEW.id,NEW.datetime;