Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:47:24+00:00 2026-05-29T09:47:24+00:00

create table #customer ( id int not null primary key, cust_name varchar(12), oldid int

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:47:24+00:00Added an answer on May 29, 2026 at 9:47 am

    Can get all of the older records for a given Customer using a CTE. Something like

    DECLARE @CustomerId int
    SET @CustomerId = 8
    
    ;
    WITH Records (Id, Cust_name, OldId) AS (
        SELECT * FROM #Customer
        WHERE Id = @CustomerId
        UNION ALL
        SELECT C.* FROM Records R
            INNER JOIN #Customer C on C.Id = R.OldId)
    SELECT * FROM Records
    

    You have options for indicating whether a newer record exists, you may want to include newer record(s) in the result set or just a bit flag, but in any case the query would be like

    SELECT * FROM #Customer WHERE OldId = @CustomerId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CREATE TABLE Customer( customer_id INT NOT NULL, first_name VARCHAR(20), last_name VARCHAR(20), PRIMARY KEY (customer_id)
create table ImagenesUsuario { idImagen int primary key not null IDENTITY } This doesn't
create table snippet( id int not null auto_increment, primary key(id), idlanguage int not null,
The following table definition: CREATE TABLE Customers( id INT NOT NULL PRIMARY KEY, name
CREATE TABLE Posts { id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(200), url VARCHAR(200) }
SQL Query: CREATE TABLE mytbl.customer( CustID int( 10 ) NOT NULL AUTO_INCREMENT , FirstName
Create Table: CREATE TABLE `category` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255)
CREATE TABLE SupplierQuote ( supplierQuoteID int identity (3504,2) CONSTRAINT supquoteid_pk PRIMARY KEY, PONumber int
I need help with the following query. create table #table1 (id int not null
We have the following table: CREATE TABLE [dbo].[CampaignCustomer]( [ID] [int] IDENTITY(1,1) NOT NULL, [CampaignID]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.