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
 SQL Server Development (2000)
 Table Comparison

Author  Topic 

mattt
Posting Yak Master

194 Posts

Posted - 2005-10-25 : 09:00:32
Hi,

I've got two tables, we'll call them A and B. All the records that are in B are also in table A, but table A also has a lot of records that are not in B. To further confuse matters, B has a proper primary key but A does not.

What I want to do is write a script which isolates all the records in A which are not in B and place them in a seperate table (which we'll call C). The records that are in both B and A are identical in both tables and there are two or three ntext fields which could be used to check whether any given record is identical across both A and B.

Does this make sense, and is it doable without a proper key in A? If so, how?

Cheers,
Matt

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-25 : 09:04:10
Create a table C with the columns you want

Then

Insert into C(columns) Select Columns from A T1 where not exists(Select * from B where col1=T.col1 and col2=T.col2 and col3=T.col3)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mattt
Posting Yak Master

194 Posts

Posted - 2005-10-25 : 09:11:10
Hi,

Sorry to be unclear in the first post, but I orginially said they were nvarchar data types when in fact they're ntext. Sql seems unwilling to let me compare ntext data .. is there any way round this?

Cheers,
Matt
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-25 : 09:13:15
You can convert it into varchar type and compare

convert(varchar(8000),col1)=convert(varchar(8000),T1.col1)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mattt
Posting Yak Master

194 Posts

Posted - 2005-10-25 : 09:16:53
That's great thanks.

I never knew you could do on-the-fly data type conversions like that, so thanks also for broadening the narrow horizons of my SQL knowledge :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-25 : 09:30:24
>>I never knew you could do on-the-fly data type conversions like that,

Better to avoid comparing those columns

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -