docs/versioned_docs/version-2.15.0/how-to/bulk-update-multiple-rows-in-table.md
Currently, the datasources in ToolJet have operation for bulk update(GUI mode) but that only works for changes made in the single row. We will soon be adding a new operation for bulk updating the multiple rows but for now we can bulk update multiple rows by creating a Custom JS query.
In this guide, We have assumed that you have successfully connected the data source. For this guide, we will be using the PostgreSQL data source as an example database, currently, this workaround can be used only for PostgreSQL and MySQL.
Let's create the query that will be getting the data from the database:
<div style={{textAlign: 'center'}}> </div>{{queries.postgresql1.data}}We will create a new Custom JS query(runjs1) that will generate SQL query for updating multiple rows.
const uniqueIdentifier = "id"
const cols = Object.values(components.table1.changeSet).map((col, index) => {
return {
col: Object.keys(col),
[uniqueIdentifier]: Object.values(components.table1.dataUpdates)[index][uniqueIdentifier],
values: Object.values(col),
};
});
const sql = cols.map((column) => {
const { col, id, values } = column;
const cols = col.map((col, index) => `${col} = '${values[index]}'`);
return `UPDATE users SET ${cols.join(", ")} WHERE id = '${id}';`;
});
return sql
:::info Here the Unique identifier is id, this is the column name that is used to identify the row in the database. Update the Unique identifier if you are using a different column name. Update table1 with the name of the table you are using. :::
<div style={{textAlign: 'center'}}> </div>Let's create a new PostgreSQL query and name it update. In SQL mode, enter {{queries.runjs1.data.join(' ')}} and Save it.