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)
 'NOT IN' with more than one column

Author  Topic 

ch9862
Yak Posting Veteran

76 Posts

Posted - 2007-04-04 : 11:45:02
Hi,

I have two tables:
declare @a table (id int, dt datetime, nme varchar(10))
insert @a
select 1, '10/08/2006', 'nme 1c' union all
select 2, '02/03/2006', 'nme 2a' union all
select 3, '05/30/2001', 'nme 3a' union all
select 4, '01/06/1997', 'nme 4a'

declare @b table (id int, dt datetime, nme varchar(10))
insert @b
select 1, '01/12/1989', 'nme 1a' union all
select 1, '12/09/1992', 'nme 1b' union all
select 1, '06/07/2001', 'nme 1c' union all
select 2, '02/03/2006', 'nme 2a'

Table a has id as PK, table b both id and dt. Table b is in effect a history table for table a. I want to insert records from a to b, but only these that aren't there already. I was thinking about using "where id, dt not in (...)" to exclude records already in b, but IN only works for one column. How else can I achieve this?

To be clear, here's what I want in b:

id dt nme
--------------------------------
1 '01/12/1989' 'nme 1a' -- old
1 '12/09/1992' 'nme 1b' -- old
1 '06/07/2001' 'nme 1c' -- old
1 '10/08/2006' 'nme 1c' -- new from a
2 '02/03/2006' 'nme 2a' -- old (one from a not inserted)
3 '05/30/2001' 'nme 3a' -- new from a
4 '01/06/1997' 'nme 4a' -- new from a


Thanks in advance for any suggestions

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-04-04 : 11:50:32
Using LEFT JOIN.

declare @a table (id int, dt datetime, nme varchar(10))
insert @a
select 1, '10/08/2006', 'nme 1c' union all
select 2, '02/03/2006', 'nme 2a' union all
select 3, '05/30/2001', 'nme 3a' union all
select 4, '01/06/1997', 'nme 4a'

declare @b table (id int, dt datetime, nme varchar(10))
insert @b
select 1, '01/12/1989', 'nme 1a' union all
select 1, '12/09/1992', 'nme 1b' union all
select 1, '06/07/2001', 'nme 1c' union all
select 2, '02/03/2006', 'nme 2a'

-- to verify for correctness of data
select a.*
from @a a LEFT JOIN @b b
on a.id = b.id and a.dt = b.dt and a.nme = b.nme
where
b.id is null and b.dt is null and b.nme is null

-- to do actual insert
Insert into @b(id, dt, nme)
select a.id, a.dt, a.nme
from @a a LEFT JOIN @b b
on a.id = b.id and a.dt = b.dt and a.nme = b.nme
where
b.id is null and b.dt is null and b.nme is null



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

ch9862
Yak Posting Veteran

76 Posts

Posted - 2007-04-04 : 12:26:47
quote:
Originally posted by harsh_athalye

Using LEFT JOIN.
Should have thought of that . Thanks!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-04-04 : 12:31:40
Or use a sub query and correlate it with NOT EXISTS

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -