Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function returns multiple columns as a single column instead of multiple columns

Tags:

I am currently writing a function in postgresql 9.04 where I am attempting to use a variable which will be used in a select statement then return the results.

The statement I have come up with is simple and works; however, all the columns are outputing to a single column instead of multiple columns.

here is my function:

create or replace function foo(IN pID integer, OUT project_id integer, OUT project_name    text, OUT project_type text, OUT project_description text, OUT project_status text)
returns setof record as

$$
select project_id, project_name, project_type, project_description, project_status from     t_projects
where project_id = $1;
$$

LANGUAGE SQL;


select foo(6) -- runs function

the current output looks like this:

"(6,"test project","inbound","inbound test","processing")"

how can I make it so the results are not concatenated together and return each column item seperately?

thank you in advance.

like image 426
richh Avatar asked Aug 15 '11 23:08

richh


People also ask

How do I return multiple columns in pandas?

Return Multiple Columns from pandas apply() You can return a Series from the apply() function that contains the new data. pass axis=1 to the apply() function which applies the function multiply to each row of the DataFrame, Returns a series of multiple columns from pandas apply() function.

How do I get values from multiple columns?

The VLOOKUP function can be combined with other functions such as the Sum, Max, or Average to calculate values in multiple columns. As this is an array formula, to make it work we simply need to press CTRL+SHIFT+ENTER at the end of the formula.

How do I make two columns in one column in pandas?

We can use Pandas' str. split function to split the column of interest. Here we want to split the column “Name” and we can select the column using chain operation and split the column with expand=True option.

How do I add multiple values in one column?

You need another table to represent the many-to-many relation. You should not insert multiple values into one column. Show activity on this post. Yes you can, in fact we do this by making 'category' (as an example) a BLOB, and thus allowing the ability to store very large subsets of data on one row.


1 Answers

you need to call the function like this:

select * from foo(6);

which will return something like this:

project_id | project_name | project_type | project_description | project_status
-----------|--------------|--------------|---------------------|----------------
         6 | test project |      inbound |        inbound test |     processing

it's a quirk of postgres that it can be called both ways and give you a result. you might want to check the docs on set returning functions some more, there are other ways to do this as well. Oh, there is a wiki page on it, written for plpgsql, but most applies to sql functions as well: http://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/pgSQL_functions

like image 55
xzilla Avatar answered Oct 19 '22 22:10

xzilla