SQL Journey

Journey of SQL Server & Microsoft Business Intelligence

Condition based Update in SQL Server

Posted by Prashant on June 9, 2010


In SQL Server conditional update can be done using CASE.

--Update employee monthly commission based on their rating
UPDATE Employees
SET Comm =
CASE
WHEN (Rating between 2 and 3)THEN (Salary * 0.1)
WHEN (Rating between 3.1 and 4)THEN (Salary * 0.15)
WHEN (Rating between 4.1 and 5)THEN (Salary * 0.25)
ELSE 0.0
END

This example is just to demonstrate a scenario. Here it examines the employee rating to determine what should be the updated Commission. The CASE expression uses range of employee rating to update the commission amount.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: