how to fetch the active record by passing the ID in Sql server -
i have following database design:
table content id status replacedid 1 c null 2 c 1 3 c 2 4 3 5 null 6 null 7 null
the logic here follows
id "1" canceled , instead id "2" created record 2 has reference id "1" in replacedid column. id 2 canceled , id "3" created , "3" canceled , "4" created. canceled records status "c" , active records status "a"
my requirement :
i have show active record id passing id (1) if canceled record othere wise same record if active record.
declare @table table( recordid int, status varchar(20), parentid int ) insert @table select 1,'c',null insert @table select 2,'c',1 insert @table select 3,'c',2 insert @table select 4,'a',3 insert @table select 5,'a',null insert @table select 6,'a',null declare @recordid int select @recordid = 1 ;with selects ( select * @table recordid = @recordid union select t.* @table t inner join selects s on t.parentid = s.recordid ) select * selects status = 'a'
Comments
Post a Comment