how can create function select all column in postgresql - postgresql

How can create an function (Stored Procedure / Stored Function) that select all column in the table in PgAdmin? That same same like this.
CREATE OR REPLACE FUNCTION GetAllUsers(IN userno integer)
RETURNS TABLE(all column) AS
$BODY$
BEGIN
RETURN QUERY
SELECT *
FROM Users W
WHERE w.UserNo = GetAllUsers.userno;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION GetAllUsers(integer)
OWNER TO postgres;

You can use returns setof users instead of returns table (...)
You also don't need an expensive PL/pgSQL function for that. A plain SQL function is enough:
CREATE OR REPLACE FUNCTION getallusers(p_userno integer)
RETURNS setof users
$BODY$
SELECT *
FROM users
WHERE userno = p_userno;
$BODY$
LANGUAGE sql VOLATILE
COST 100
ROWS 1000;

Related

Create a stored procedure to Delete Records postgres

I have created a function to delete multiple records.In our table contain id as type uuid.
We get the input is like array of ids.
CREATE OR REPLACE FUNCTION public.deletetVersion(item_list uuid[])
RETURNS TABLE(id uuid[])
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
RETURN QUERY
DELETE FROM version WHERE id = ANY(item_list);
END;
$BODY$;
SELECT * from deletetVersion(Array['b6ad1912-e4f1-4419-831a-c70df89ffd63','877898f0-2f3f-4890-a658-898e35ffee3a'])
But i got an error like:
Anyone please help me
ERROR: function deletetversion(text[]) does not exist
it is because the
Array['b6ad1912-e4f1-4419-831a-c70df89ffd63','877898f0-2f3f-4890-a658-898e35ffee3a']
is treated as text[]
try the following
Array['b6ad1912-e4f1-4419-831a-c70df89ffd63'::uuid,'877898f0-2f3f-4890-a658-898e35ffee3a'::uuid]
as a parameter to your function
for example
CREATE OR REPLACE FUNCTION public.test_uuid(item_list uuid[])
RETURNS TABLE(id uuid[])
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
RETURN QUERY
SELECT item_list;
END;
$BODY$;
SELECT * from test_uuid(Array['b6ad1912-e4f1-4419-831a-c70df89ffd63'::uuid])
In case of deletion
CREATE OR REPLACE FUNCTION public.test_uuid(item_list uuid[])
RETURNS VOID
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
BEGIN
RETURN QUERY
DELETE from tableName WHERE id = ANY(item_list);
END;
$BODY$;
Your function should return either setof uuid - i.e. a table of uuid-s - or uuid[]. I would prefer the first. You do not need PL/pgSQL, plain SQL is enough. So the function is:
create or replace function public.deletetVersion(item_list uuid[])
returns setof uuid language 'sql' as
$$
delete from version where id = any(item_list) returning id;
$$;
The version returning an array is a bit more complex:
create or replace function public.deletetVersion(item_list uuid[])
returns uuid[] language 'sql' as
$$
with t(d_id) as
(
delete from version where id = any(item_list) returning id
)
select array_agg(d_id) from t;
$$;
And - as #Ibrahimshamma says - you may need to cast the argument to uuid[].

parameter name used more than once in postgresql

My project used MS SQL, and postgresql so I can't change my parameter to another name, something like below
CREATE OR REPLACE FUNCTION myfunction( ReservationNo integer)
RETURNS TABLE("reservationno" integer) AS
$BODY$
begin
RETURN QUERY
select
rr.ReservationNo
FROM Mytable rr
WHERE rr.ReservationNo = myfunction.ReservationNo;
end
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION myfunction(integer)
OWNER TO postgres;
But it give me an error
parameter name "reservationno" used more than once
How can I fix it?
You can change it to a SQL function, which doesn't have this problem:
CREATE OR REPLACE FUNCTION myfunction( ReservationNo integer)
RETURNS TABLE("reservationno" integer) AS
$BODY$
select rr.ReservationNo
FROM Mytable rr
WHERE rr.ReservationNo = myfunction.ReservationNo;
$BODY$
LANGUAGE sql stable
;

How to get entire table data or multiple rows returned from a function in PG/PLSQL with pgAdmin 4.2.?

I tried using setof and table. While creating function in pgAdmin 4.2 there is no return type called setof or table. If I create function with setof and table name as a selected return type, it only returns one row of table.
CREATE FUNCTION pgsql_returnrecords() RETURNS SETOF RECORD(name char, city, char, id integer) AS
$BODY$
DECLARE
rec RECORD;
BEGIN
select name,city,id INTO rec from test;
return next rec;
END;
$BODY$ language plpgsql;
I want my function to return table data with all rows and columns.
It's either returns setof record or returns table(....) or setof table_name With returns setof record you have to specify the column names when using the function.
You are also not returning a complete result, because you only fetch a single row, put it into the record and return that. To return a real "set" you need to use return query in PL/pgSQL. But such a function is much better written as a SQL function:
CREATE FUNCTION pgsql_returnrecords()
RETURNS table(name text, city text, id integer)
AS
$BODY$
select name,city,id
from test;
$BODY$
language sql;
If you want to always return a complete row from the table test you can simplify that using returns setof test instead of returns table(..)
CREATE FUNCTION pgsql_returnrecords()
RETURNS setof test
AS
$BODY$
select *
from test;
$BODY$ language sql;
Or, if you insist on PL/pgSQL:
CREATE FUNCTION pgsql_returnrecords()
RETURNS table(name text, city text, id integer)
AS
$BODY$
BEGIN
return query
select name,city,id
from test;
END;
$BODY$
language plpgsql;
In both cases you have to use the function like a table in the FROM clause:
select *
from pgsql_returnrecords() ;

dont know what this function does

I have a database that is named Dvdrental and in that database there is a function that is named public.film_not_in_stock, but I don't understand what it does.
-- Function: public.film_not_in_stock(integer, integer)
-- DROP FUNCTION public.film_not_in_stock(integer, integer);
CREATE OR REPLACE FUNCTION public.film_not_in_stock(
IN p_film_id integer,
IN p_store_id integer,
OUT p_film_count integer)
RETURNS SETOF integer AS
$BODY$
SELECT inventory_id
FROM inventory
WHERE film_id = $1
AND store_id = $2
AND NOT inventory_in_stock(inventory_id);
$BODY$
LANGUAGE sql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION public.film_not_in_stock(integer, integer)
OWNER TO postgres;
It's a LANGUAGE SQL function. Not plpgsql.
SQL-language functions implicitly return the results of the last query they run.
Your function, if it was rewritten in plpgsql, would do
$BODY$
BEGIN
RETURN QUERY
SELECT inventory_id
FROM inventory
WHERE film_id = $1
AND store_id = $2
AND NOT inventory_in_stock(inventory_id);
$BODY$
LANGUAGE plpgsql VOLATILE
but in a LANGUAGE SQL function the RETURN is implicit.

PostgreSQL function returns all records

How to modify existing function to return all rows from table sd_users?
CREATE OR REPLACE FUNCTION getusers()
RETURNS sd_users AS
$BODY$
DECLARE
groups sd_users;
BEGIN
SELECT * INTO groups FROM sd_users;
RETURN groups;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION getusers()
OWNER TO postgres;
Invoke the function code:
SELECT getusers()
This code returns only first record, how to make it to return all records?
You are looking for setof. And it can be plain SQL in instead of plpgsql
create or replace function getusers()
returns setof sd_users as $body$
select * from sd_users;
$body$ language sql stable