Swiftybase
Function StackDatabasePostgresExamples

Update Returns

Example 1: Update and Return

In this example, we'll use the Postgres Execute function with the Type set to Update Returns to modify data and return the id of the updated row.

Add Postgres Execute Function

Start by adding the Postgres Execute function. Give it a Result Name and set the Type to Update Returns.

Write the SQL Query

Write the update query you want to run. Since we want to return the updated row's id, use the RETURNING clause.

Assume we have a users table and want to update the name of a user by their id:

CREATE TABLE public.users (
  id bigint GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  created_at timestamp with time zone NOT NULL DEFAULT now(),
  name text NULL,
  email text NULL,
  age integer NULL,
  activated boolean NULL,
  updated timestamp without time zone NULL,
  CONSTRAINT users_pkey PRIMARY KEY (id)
);

Update query with returning:

UPDATE users SET name = $1 WHERE id = $2 RETURNING id

Fill the Arguments

Provide the values for the update:

["$name", 15]

Define a Type

Since the query returns the id, you need to define a primitive type like Int64 to capture it.

Testing

Once the query runs successfully, the returned id will be available in the Data field.

On this page