mr-santos
mr-santos

Reputation: 73

Insert a result from a stored procedure in postgresql

I'm trying to understand how to deal with procedures in Postgresql.

I get the idea of creating a function that returns a variable. What I don't get is how I can use such variable, for instance, in an insert.

Imagine this, I have a function called getName(), that returns a variable $name$.

What I want is to insert such variable in another table... How can I do this?

Upvotes: 0

Views: 60

Answers (1)

user330315
user330315

Reputation:

If the function returns a single value, you can use it anywhere a constant could be used.

insert into some_table (id, name)
values (42, get_name());

this is the same as using a built-in function:

insert into some_table (id, modified_at)
values (42, now());

It can be used the same way in an update statement

update some_table
  set name = get_name()
where id = 42;

Upvotes: 1

Related Questions