0

I'm trying to write a procedure, which inserts a row in a table and then returns an assigned id.

Table example:

CREATE TABLE sometable (
UUID Id,
varchar field1,
varchar field2)

My function:

CREATE OR REPLACE FUNCTION insert_sometable(varchar pfield1, varchar pfield2)
  RETURNS UUID AS  $$
DECLARE
pid UUID;
BEGIN
pid=uuid_generate();
insert into sometable(id, field1, field2) values (pid, pfield1, pfield2);
return pid;
END;
$$
LANGUAGE plpgsql;

And i want it to use it like that:

select pid from insert_sometable('_field1_value', '_field2_value')

But now i got no column name of its result, so i can't access it in my ESB service. I want column called "pid" in result set. Seems like easy task, but i didn't found any simple solution. Any suggestions?

2 Answers 2

1

I think you're just trying to alias the returned column name. If so:

select pid 
from insert_sometable('_field1_value', '_field2_value') AS inserted_row(pid);

PostgreSQL permits you to write shorthand for this for functions that return only one column:

select pid 
from insert_sometable('_field1_value', '_field2_value') pid;
Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't use a function whose return value is atomic (not a row) in the FROM clause (it's possible, but makes your query less readable). You can add an alias in the SELECT clause too:

select insert_sometable('_field1_value', '_field2_value') as pid;

1 Comment

Thanks, pozs. You are so fast.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.