create table #customer (
id int not null primary key,
cust_name varchar(12),
oldid int null
)
insert into #customer values(1,'XYZ',null)
insert into #customer values(2,'XYZ',1)
insert into #customer values(3,'XYZ',2)
insert into #customer values(4,'ABC',null)
insert into #customer values(5,'ABC',4)
insert into #customer values(6,'DEF',null)
insert into #customer values(7,'DEF',6)
insert into #customer values(8,'DEF',7)
insert into #customer values(9,'DEF',8)
select * from #customer
-- output
id cust_name oldid
----------- ------------ -----------
1 XYZ NULL
2 XYZ 1
3 XYZ 2
4 ABC NULL
5 ABC 4
6 DEF NULL
7 DEF 6
8 DEF 7
9 DEF 8
This is a simulation when a record is renewed, the new record stores the id of its older record. The chain continues and goes down to the very first record created for that customer.
What I want is I issue a command like
select * from #customer where id=3
This should put not only the record with id=3 but all its old version, that is records 2 and 1 also.
select * from #customer id=4
should pull only that record (oldid = null)
Enhancement (optional): If someone issue a commond
select * from #customer where id=8
I would somehow like to indicate, a new record exist for this customer. How can I do that? Assume I am going to use in ASP.NET application.