update [users] set [name] = @par0 where [users].[name] = @par1; -- params: ['Mr. Dan', 'Dan']
The object that you pass to update should have keys that match column names in your database schema.
Values of undefined are ignored in the object: to set a column to null, pass null.
This method allows you to return values from the rows affected by the query. MSSQL supports returning inserted (new row values) and deleted (old row values) values.
If no fields are specified, all inserted values will be returned by default.
// Update cars and return all new valuesconst updatedCars: Car[] = await db.update(cars) .set({ color: 'red' }) .output() .where(eq(cars.color, 'green'));// Update cars and return all new valuesconst updatedCars: Car[] = await db.update(cars) .set({ color: 'red' }) .output({ inserted: true }) .where(eq(cars.color, 'green'));// Update cars and return all old valuesconst updatedCarsIds: { deleted: Car }[] = await db.update(cars) .set({ color: 'red' }) .output({ deleted: true }) .where(eq(cars.color, 'green'));// Update cars and return partial old and new valuesconst beforeAndAfter: { deleted: { oldColor: string }, inserted: { newColor: string } }[] = await db.update(cars) .set({ color: 'red' }) .output({ deleted: { oldColor: cars.color }, inserted: { newColor: cars.color } }) .where(eq(cars.color, 'green'));
update [cars] set [color] = 'red' output INSERTED.[name], INSERTED.[color] where [cars].[color] = 'green';update [cars] set [color] = 'red' output INSERTED.[name], INSERTED.[color] where [cars].[color] = 'green';update [cars] set [color] = 'red' output DELETED.[name], DELETED.[color] where [cars].[color] = 'green';update [cars] set [color] = 'red' output INSERTED.[color], DELETED.[color] where [cars].[color] = 'green';