IF object_id('tempdb..#A') IS NOT NULL DROP TABLE #A
IF object_id('tempdb..#B') IS NOT NULL DROP TABLE #B
CREATE TABLE #A (fname varchar(20), lname varchar(20))
CREATE TABLE #B (fname varchar(20), lname varchar(20))
INSERT INTO #A
SELECT 'Kevin', 'XP'
UNION ALL
SELECT 'Tammy', 'Win7'
UNION ALL
SELECT 'Wes', 'XP'
UNION ALL
SELECT 'Susan', 'Win7'
UNION ALL
SELECT 'Kevin', 'Win7'
SELECT * FROM #A
INSERT INTO #B
SELECT a.fname, a.lname FROM #A a
WHERE a.fname NOT IN (SELECT fname from #B)
SELECT * FROM #B
DELETE FROM #B
INSERT INTO #B
SELECT a.fname, a.lname FROM #A a
LEFT OUTER JOIN #B b ON a.fname = b.fname
WHERE a.fname NOT IN (SELECT fname from #B)
SELECT * FROM #B
Both of these examples copy all 5 records to the new table.
I only want to see one unique fname so only one Kevin should show up.
Why don't these work, or is there a better way to do it?
It seems like such a simple thing.