Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 comapring two rows from same table in sql server 2000

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-05-16 : 08:27:43
uma writes "I have a requirement in sql server 2000 to compare 2 records from same table by ignoring primary_key columns
and display column names which are having different values

( I have to compare 9 tables and if client add more columns to tables in next version
this compare proc needs to take care of new fields also without changing procedure)

for example



drop table my_test
go
create table my_test
( testno int not null,
test_name varchar(100) not null,
effective_date datetime not null,
val1 int null,
val2 int null,
val3 int null)

alter table my_test
add constraint pk_my_test primary key(testno,test_name,effective_date)

insert into my_test
values(1,'my test case1','20040101',1,1,1)

insert into my_test
values(2,'my test case1','20040101',1,1,2)

insert into my_test
values(1,'my test case1','20040102',2,2,2)

insert into my_test
values(2,'my test case1','20040102',2,3,2)


select * from my_test


I want to compare these two records only val1,val2,val3 columns( by ignoring primary key columns )and wants to display
'there is difference in val3'

select val1,val2,val3 from my_test where testno =1 and test_name = 'my test case1' and effective_date = '20040101'
select val1,val2,val3 from my_test where testno =2 and test_name = 'my test case1' and effective_date = '20040101'

-- val2 is different in below queries so I have to display val2 is different

select val1,val2,val3 from my_test where testno =1 and test_name = 'my test case1' and effective_date = '20040102'
select val1,val2,val3 from my_test where testno =2 and test_name = 'my test case1' and effective_date = '20040102'

in procedure we are going to pass

testno1
testno2
test_name
effective_date

test_name,effective_date are same for two records


Thanks,
Uma"
   

- Advertisement -