Merge Update Sample




Create table MinuteCategory([Minute] int, MinuteCategory varchar(50))
Insert into MinuteCategory values (1,'a1'),(33,'a1'),(65,'a1')
Create table MinuteLookup( MinuteCategoryId int identity(1,1), MinuteLow int, MinuteHigh int, MinuteCategory varchar(50))
Insert into MinuteLookup values(1,15,'<15min'),(16,30,'<30 min'),(31,45,'<45 min'),(46,60,'<60 minutes'),(61,75,'<1 hour 15 minutes')


Merge MinuteCategory mc 
Using MinuteLookup ml on mc.[Minute] between ml.MinuteLow and ml.MinuteHigh
When Matched Then
UPDATE 

SET MinuteCategory = ml.MinuteCategoryId;


select * from MinuteCategory mc inner join MinuteLookup ml on mc.MinuteCategory = ml.MinuteCategoryId

----Clean up
drop table MinuteCategory,MinuteLookup


 
Advertisements


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