Solved

SQL deLETE QUERY

Posted on 2011-03-19
4
163 Views
Last Modified: 2012-05-11
This:

SELECT * FROM Product_Categories WHERE Product_ID = '101'

Returns me 2 items one where Category_ID = 0 and another where it is something different then 0

How would I delete all items in the table where  Category_ID = 0 IF there is at least one OTHER item where  Category_ID is NOT 0
0
Comment
Question by:vbnetcoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 35172006
-- Run this as a SELECT first to make sure it only affects the rows you want to delete.  I also suggest you backup this table first.

select Product_ID
-- delete
from Product_Categories
where Category_ID = 0
  and Product_ID in (
         select Product_ID
         from Product_Categories
         group by Product_ID
         having count(*) > 1
  )

0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35172009
you can change the top line to SELECT * instead of SELECT Product_ID
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35172017
Delete from FROM Product_Categories where (Category_ID = 0) and (select count(Category_ID) from Product_Categories where Category_ID <> 0) > 1
0
 

Author Closing Comment

by:vbnetcoder
ID: 35172044
ty
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question