Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Data between two identical tables

Status
Not open for further replies.

captainpicart

Programmer
Mar 17, 2011
4
Hi Techies,

I have two identical tables (1) Employee (2) PreviousEmployee. Now, I need to create only one query/storedProcedure which has to do the following:

1. Get only records which exist only in Employee table but they do not exist in PreviousEmployee table. These records should contain a new column called "Status" and Its value should be "Added".

2. After the above result set, only get modified rows between two tables. For example: employeeID 0067 exist in both tables. and it has been modified in either of these two tables. Still get the modified row from Employee table. THe value of "Status" column for each row would be "Updated".

Thanks!

 
Check
You can do
Code:
select AllFields,  cast('Added' as varchar(10)) as Status
into EmployeeChanges
 from Employee E where not exists (select 1 from
PreviousEmployee P where P.EmployeeID = E.EmployeeID)

insert into EmployeeChanges
select *, 'Updated' as Status from Employee E
INNER JOIN PreviousEmployee P on E.EmployeeID = P.EmployeeID
where checksum(E.Col1, E.Col2, ...) <> checksum(P.Col1, P.Col2,..)

PluralSight Learning Library
 
This is Great! Thanks Markros!

Can you recommend really good books which teach solid T-SQL programming?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top