2

I am new to SQL, so please try not to be overly critical about my question, I need to create a function which would return me a table (say for example "machine") , which would have a column called "aggtablename" and the rows would be filled with values derived from a database. Here is what i tried and the following error came....so please help me in making my syntax correct, THANKS..

CREATE FUNCTION aggtable() RETURNS TABLE (machineid, serveraggtablename)
AS $table$  
BEGIN     
  RETURN QUERY    
  SELECT m.machineid, m.serveraggtablename   
  FROM machine m 
   END; 
 $table$  
LANGUAGE plpgsql;  

select aggtable();

ERROR: function aggtable() does not exist LINE 1: select aggtable(); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.

2 Answers 2

0

The arguments of the table you're returning do not have any type.

try adding a type such as machineid int.

check this post

How can a Postgres Stored Function return a table

Sign up to request clarification or add additional context in comments.

1 Comment

Link only answers are not encouraged, please post some code and explain
0

Try this

CREATE TYPE machineType as (machineid int, serveraggtable character varying);

CREATE FUNCTION aggtable() RETURNS SETOF machineType AS 
'SELECT m.machineid, m.serveraggtablename FROM machine m;' 
LANGUAGE 'sql';

SELECT * FROM aggtable();

https://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/pgSQL_functions

2 Comments

Thanks for your help but, now there is one more issue, i changed the syntax...now the error i am getting is : CREATE TYPE machineType as (machineid int, serveraggtable character varying); CREATE FUNCTION aggtable() RETURNS SETOF machineType AS $$ BEGIN SELECT id, serveraggtablename FROM machine ; END; $$ LANGUAGE 'plpgsql'; SELECT * FROM aggtable(); ERROR: query has no destination for result data
@user3688338 That is another question. If this answer solved the posted question then accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.