PostgreSql - Having trouble displaying my table using perform statment in my function - postgresql

This is the perform statement I use:
perform * from table1;
But when I execute the function the doesn't display the table onto the terminal. There are no errors produced, the table just doesn't show up.
My function:
CREATE OR replace FUNCTION addToTable (number INTEGER,NAME VARCHAR(15))
RETURNS void AS $$
BEGIN
INSERT INTO table1
VALUES (
number
,NAME
);
perform *
FROM table1;
END;$$
LANGUAGE plpgsql;

CREATE OR replace FUNCTION addToTable_1 (num INTEGER,nam VARCHAR(15))
RETURNS setof table11 AS $$
BEGIN
EXECUTE format( 'insert into table11 values(%s,%s) ',num,quote_literal(nam));
RETURN QUERY
EXECUTE 'select * from table11';
END;$$
LANGUAGE plpgsql;
Call it : select * from addToTable_1(2,'User')

Related

Save execute results into a table

Below is a simplified postgres stored procedure I am trying to run:
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute(' select *
into my_schema.MyTable_'||suffix||'
From my_schema.MyTable
');
end;
$$
language plpgsql;
When I attempt to run using something like:
call my_schema.tst('test');
I get this error Invalid operation: EXECUTE of SELECT ... INTO is not supported;
Is it possible to execute a dynamic query that creates a new table? I have seen examples that look like:
Execute('... some query ...') into Table;
but for my use case I need the resulting tablename to be passed as a variable.
In PostgreSQL you can use INSERT INTO tname SELECT...
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute ' INSERT INTO my_schema.MyTable_'||suffix||' SELECT *
FROM my_schema.MyTable
';
end;
$$
language plpgsql;
or Use CREATE TABLE tname AS SELECT..., :
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute ' CREATE TABLE my_schema.MyTable_'||suffix||' as SELECT *
FROM my_schema.MyTable
';
end;
$$
language plpgsql;

Return table in stored procedure postgresql

I want to return the whole table with stored procedure not function
my code is:
CREATE OR REPLACE PROCEDURE public.test()
RETURN TABLE (
id numeric
, test varchar
) AS -- Results -- remove this
$func$
BEGIN
RETURN QUERY
SELECT *
FROM public.test
END
$func$ LANGUAGE plpgsql;
but its not works.
Stored procedures aren't meant to return anything, use a function. And you don't need PL/pgSQL for that either:
create or replace FUNCTION public.test()
returns TABLE (id numeric, test varchar)
AS
$func$
SELECT *
FROM public.test;
$func$
LANGUAGE sql;
As you return all columns of one table, you can also use returns setof
create or replace FUNCTION public.test()
returns setof public.test
AS
$func$
SELECT *
FROM public.test;
$func$
LANGUAGE sql;
Then use it like a table:
select *
from test();

Query returned successfully but i dont see anyway

I'm new to postgresql, i have a procedure block this is working but i dont see any table or column, what this problem.
CREATE PROCEDURE list3(
)
LANGUAGE sql
AS $$
select * from stok
$$;
call list3()
Functions return values.
Example:
create table table_example(col1 varchar(100),col2 int);
CREATE OR REPLACE FUNCTION get_table_example()
RETURNS TABLE (
l_col1 VARCHAR,
l_col2 INT
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT *
FROM table_example;
END;
$$
select * from get_table_example();

PostgreSQL Function Type reuse

I have one function, that returns a table. I need another function to inherit same output type, like here:
CREATE OR REPLACE FUNCTION t_report (pMedia text[])
RETURNS TABLE (
media text) AS
$func$
DECLARE
sql text;
BEGIN
sql := 'SELECT
tStreams.Media
FROM
(SELECT
''Audio'' as Media
UNION ALL
SELECT
''Video'' as Media
UNION ALL
SELECT
''App Sharing'' as Media) tStreams
WHERE tStreams.Media in (select unnest($1))';
RETURN QUERY EXECUTE sql
USING pMedia
;
END
$func$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION t_report (pMedia text)
RETURNS TABLE (
media text) AS
$func$
BEGIN
RETURN QUERY EXECUTE 'SELECT * FROM t_report(ARRAY[$1])' USING pMedia
;
END
$func$ LANGUAGE plpgsql;
SELECT *
FROM t_report('Audio');
This works fine, but I need the second function to inherit type from the first.
Is it possible to do straight or I need to create specific type and put it in as RETURNS SETOF MyType?

WITH clause containing a data-modifying statement must be at the top level SQL-state: 0A000

I wrote function, which using WITH construction with insert into table like this:
CREATE OR REPLACE FUNCTION test_func()
RETURNS json AS
$BODY$
begin
return (
with t as (
insert into t(id)
select 1
returning *
)
select '{"a":"a"}'::json
);
end;
$BODY$
LANGUAGE plpgsql VOLATILE;
select test_func()
Thats return error:
ERROR: WITH clause containing a data-modifying statement must be at the top level
SQL-состояние: 0A000
If execute
with t as (
insert into t(id)
select 1
returning *
)
select '{"a":"a"}'::json
Result without errors.
Why this take place and how get round this?
You are doing subselect on that query, with is why it doesn't work.
This won't work either:
select * from (
with t as (
insert into t(id)
select 10
returning *
)
select '{"a":"a"}'::json
) as sub
There are a few solutions to this.
a) Declare it as returning setof and use return query
CREATE OR REPLACE FUNCTION test_func()
RETURNS setof json AS
$BODY$
begin
return query
with t as (
insert into t(id)
select 7
returning *
)
select '{"a":"a"}'::json;
end;
$BODY$
LANGUAGE plpgsql VOLATILE;
b) Declare it as language sql
CREATE OR REPLACE FUNCTION test_func()
RETURNS json AS
$BODY$
with t as (
insert into t(id)
select 8
returning *
)
select '{"a":"a"}'::json;
$BODY$
LANGUAGE sql VOLATILE;
c) Declare output variable(s) in argument list and assign result to them
CREATE OR REPLACE FUNCTION test_func(OUT my_out_var json)
AS
$BODY$
begin
with t as (
insert into t(id)
select 9
returning *
)
select '{"a":"a"}'::json INTO my_out_var;
end;
$BODY$
LANGUAGE plpgsql VOLATILE;