dmr
dmr

Reputation: 22393

Delete from select in sql server 2000?

I want to delete anything that is returned by this select query:

SELECT  u.*
FROM    (
        SELECT  userName, groupId, MAX(userId) AS maxId
        FROM    userTable
    where userName <> '' and userName is not null
        GROUP BY
                userName, groupId
        HAVING  COUNT(*) > 1
        ) q
JOIN    userTable u
ON      u.userName = q.userName
        AND u.groupId= q.groupId
        AND u.userId <> q.maxId)

How can I do this?

Upvotes: 1

Views: 3359

Answers (2)

THEn
THEn

Reputation: 1938

just replace SELECT u.* with DELETE U

Upvotes: 5

Michael Haren
Michael Haren

Reputation: 108356

Structure your delete like this:

DELETE U
FROM U ...
JOIN Q ...
WHERE Foo ...

Or, if you already have a spiffy query in hand, you can do this:

DELETE Usertable WHERE userId IN (
   SELECT UserID FROM ... /* big complex query here */
)

Upvotes: 10

Related Questions