Skip to content

Commit 779d69b

Browse files
PierBoverporsager
authored andcommitted
Docs for multiple updates in one query
Like we chatted on Gitter, I've added this bit of information. I tried to maintain the style of the code and SQL to the other examples of the docs. Hope the location is ok, I didn't know where to put it.
1 parent 52dfe9a commit 779d69b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ sql`
232232
update users set "name" = $1, "age" = $2 where user_id = $3
233233
```
234234

235+
### Multiple updates in one query
236+
It's possible to create multiple udpates in a single query. It's necessary to use arrays intead of objects to ensure the order of the items so that these correspond with the column names.
237+
```js
238+
const users = [
239+
[1, 'John', 34],
240+
[2, 'Jane', 27],
241+
]
242+
243+
sql`
244+
update users set name = update_data.name, age = update_data.age
245+
from (values ${sql(users)}) as update_data (id, name, age)
246+
where users.id = update_data.id
247+
`
248+
```
249+
235250
### Dynamic values and `where in`
236251
Value lists can also be created dynamically, making `where in` queries simple too.
237252
```js

0 commit comments

Comments
 (0)