I want to create a table view using a function that returns a temporary table...
For example, I have a function.
create or replace function colpivot(
out_table varchar
) returns void as $$
declare
in_table varchar;
begin
create table as select * from employees;
end;
Now I want to create a view using the temporary table (out_table) given by above function...
Is there a way to do this?
I would modify your query as follows
create or replace function colpivot(
out_table varchar ) returns void as $$
begin
select * from employees into out_table ;
execute 'CREATE OR REPLACE VIEW newView AS ' || out_table;
end;
$$ LANGUAGE plpgsql;
Related
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();
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')
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;
For example: I have a VIEW called "view1" which contains 'name' and 'slno' columns, now i want it to be display using FUNCTION called "f1" as shown below:
--Function
create or replace function f1(viewname varchar)
returns table (name varchar,slno integer) as
$body$
begin
return query
select * from viewname;
end;
$body$
language plpgsql;
This is dynamic SQL, so you need EXECUTE.
RETURN QUERY EXECUTE format('SELECT * FROM %I', "name");
Separately, that's a weird thing to want to do.
I want to create a new table with plpgsql, but I want that the user gives the name of the table.My code generate a table with the name of tbname
this is my code:
CREATE OR REPLACE FUNCTION "Object_gen"(tablename text)
RETURNS void AS
$BODY$DECLARE
tbname text;
BEGIN
tbname:=tablename;
CREATE TABLE tbname(Surrogat uuid);
END$BODY$
LANGUAGE plpgsql;
How can I solve my problem? somehow that the name of my table comes from the user
You need to use dynamic SQL:
CREATE OR REPLACE FUNCTION "Object_gen"(tablename text)
RETURNS void AS
$BODY$
BEGIN
execute 'CREATE TABLE '||tablename||' (Surrogat uuid)';
END
$BODY$
LANGUAGE plpgsql;