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 2008 Forums
 Transact-SQL (2008)
 removing data via join

Author  Topic 

neil_akoga
Yak Posting Veteran

56 Posts

Posted - 2009-06-29 : 07:25:00
i've got 2 tables tblUsers and tblProfiles
both can be joined via a userID column

tblProfiles only has some of the users from tblUSers (e.g. 5000 users but only 3000 have profile entries in tblProfiles) How could i select the rows from tblUsers that wouldn't appear if i joined the 2 tables via userID

i know this is probably easy and i guess it involves 'not in' or something similar but it's monday and my head is pounding :(

once i've done this would it be pretty straightforward to turn the select into a delete statment?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-29 : 07:38:34
Not tested!
-- select users without profile
select tu.*
from tblUSers tu
left join tblProfiles tp on tp.userID = tu.userID
where tp.userID IS NULL

-- delete users without profile
delete tu
from tblUSers tu
left join tblProfiles tp on tp.userID = tu.userID
where tp.userID IS NULL

Fred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-06-29 : 07:45:01
Peso wrote a blog about all the different methods of finding (in one but not in another) type problems. Comparing the relative efficiencies. interesting reading:

http://weblogs.sqlteam.com/peterl/archive/2009/06/12/Timings-of-different-techniques-for-finding-missing-records.aspx



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -