Executing multiple sql commands with one generated function #2821
Unanswered
krishna15898
asked this question in
Q&A
Replies: 1 comment
-
To execute multiple queries in a single function, you'll want to use a database transaction. Executing multiple queries outside of a transaction may result in race conditions, so you'll want to do this anyways. If you're using PostgreSQL, you don't need the -- name: DeleteItem :one
DELETE FROM items WHERE id = ?
RETURNING position;
-- name: UpdatePositions :execrows
UPDATE items SET position = position - 1
WHERE item.position > sqlc.arg(position); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
My table of items looks like this.
I am writing a delete API which deletes an item and should update the positions of the remaining items so there are no gaps. For example, after I delete
ball
, I would like the table to look likeIn other words - it should delete the target item and reduce the position of all the items with positions greater than that of the target item by one.
Below is the way I am currently doing it -
And calling the functions shown -
I am first getting the item to be deleted to get its position, deleting the item, and executing an update command with the position found in the first step. I want to reduce the function calls I make for this delete operation.
Questions -
GetItem
call by writing a single SQL statement to get and update item positions?Beta Was this translation helpful? Give feedback.
All reactions