Sure, here is how to bulk update multiple rows in SQLServer at a time with different matching conditions:
SQL
USE MyDatabase;
UPDATE Customers
SET
FirstName = CASE CustomerID
WHEN 1 THEN 'John'
WHEN 2 THEN 'Jane'
ELSE FirstName
END,
LastName = CASE CustomerID
WHEN 1 THEN 'Doe'
WHEN 2 THEN 'Doe'
ELSE LastName
END
WHERE CustomerID IN (1, 2);
This code will update the Customers table to change the first and last names of the customers with the IDs 1 and 2. The
CASE statement is used to specify different values for the FirstName and
LastName columns, depending on the value of the CustomerID column. The
WHERE clause is used to specify that the updates should only be applied to the rows where the
CustomerID column is equal to 1 or 2.
The UPDATE statement can also be used to update multiple rows with the same value for a particular column. For example, the following code would update the
CustomerID column of all rows in the Customers table to 100:
SQL
UPDATE Customers
SET CustomerID = 100;
The UPDATE statement is a powerful tool for updating data in SQL Server. It can be used to update a single row or multiple rows, and it can be used to update a single column or multiple columns.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is how to bulk update multiple rows in SQL Server at a time with different matching conditions:
SQL
This code will update the
Customerstable to change the first and last names of the customers with the IDs 1 and 2. TheCASEstatement is used to specify different values for theFirstNameandLastNamecolumns, depending on the value of theCustomerIDcolumn. TheWHEREclause is used to specify that the updates should only be applied to the rows where theCustomerIDcolumn is equal to 1 or 2.The
UPDATEstatement can also be used to update multiple rows with the same value for a particular column. For example, the following code would update theCustomerIDcolumn of all rows in theCustomerstable to 100:SQL
The
UPDATEstatement is a powerful tool for updating data in SQL Server. It can be used to update a single row or multiple rows, and it can be used to update a single column or multiple columns.