Skip to content

Instantly share code, notes, and snippets.

@serjKim
Last active August 15, 2019 14:32
Show Gist options
  • Save serjKim/caaabe2785651affa7c8d3e63ef38704 to your computer and use it in GitHub Desktop.
Save serjKim/caaabe2785651affa7c8d3e63ef38704 to your computer and use it in GitHub Desktop.
CREATE TABLE #MyLocks
(
	hash VARCHAR(50),
	age int,
)

ALTER TABLE #MyLocks
ADD UNIQUE (hash)
 
INSERT INTO #MyLocks (hash, age)
	values ('111', 5),  ('abc', 3), ('222', 14), ('333', 14), ('444', 14)

--delete from #MyLocks where hash = '111'

--select * from #MyLocks where hash = '111' and (@currentAge -  age) > @delta

declare 
	@currentAge int = 45, 
	@delta int = 5

merge #MyLocks target
using (select '333' as hash) as source(hash)
	on source.hash = target.hash and (@currentAge - target.age) > @delta
when matched then
	update set age = @currentAge
when not matched then
	insert (hash, age) values ('333', @currentAge);
 

 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment