Sql Server Insert a new row if 1 value does not exist in dest table -


i have 2 tables, , b. want insert name , 'notintime' table b if name not falling period between time_start , time_end.

eg: time = 10:30

table name time_start (datetime) time_end (datetime) 12:00 14:00 10:00 13:00 b 09:00 11:00 b 10:00 11:00 c 12:00 14:00 d 16:00 17:00  table b name indicator intime b intime 

if run query should add following table b

c notintime d notintime 

this add in time , not in time table_b

declare @now time = '10:30'  insert table_b(name, indicator) select  a.name, case when b.chk = 1 'intime' else 'notintime' end  ( select distinct name table_a ) outer apply (select top 1 1 chk table_a  @now between time_start , time_end , a.name = name) b 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -