SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete Row from one table if other table with multiple rows meet the condition


Delete Row from one table if other table with multiple rows meet the condition

Author
Message
RamSteve
RamSteve
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 Visits: 635
In TableA We don't have Product Column, It only in TableB
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26858 Visits: 17557
RamSteve (10/7/2013)
In TableA We don't have Product Column, It only in TableB


You really need to quote the original post in your responses so we have a clue what you are referring to.

We are all shooting in the dark here because we have no knowledge of your system and you didn't provide ddl for us. Please take a few minutes and read the first article in my signature.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Keith Tate
Keith Tate
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1042 Visits: 979
You need to provide some DDL for this problem. How about some create table statements with actual column names and real data with expected results.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
parulprabu
parulprabu
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1488 Visits: 136
Hi,

Try with this code



delete from tableA where ConsumerID in(
select ConsumerID from tableB where ConsumerID not in(
select ConsumerID from tableB
where DeleteFlag = 0))


T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 599
CREATE TABLE [A]
(consumerID INT, XXXX INT, AAAAA INT, BBBB INT)

CREATE TABLE [B]
(consumerID INT, Product INT, DeleteFlag BIT, DDDD INT)

INSERT INTO A VALUES (99,1,2,3)
INSERT INTO A VALUES (100,2,3,4)

INSERT INTO B VALUES (99,1,1,4)
INSERT INTO B VALUES (99,2,1,5)
INSERT INTO B VALUES (99,1,0,6)
INSERT INTO B VALUES (99,5,1,3)
T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 599
DELETE A FROM A INNER JOIN B ON A.consumerID = B.consumerID WHERE A.consumerID = 99 AND B.deleteflag = 1
gudukrmca
gudukrmca
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 218
Using Co-related query:

DELETE FROM tablea
WHERE ConsumerID IN
(SELECT ConsumerID FROM Tableb b WHERE tablea.ConsumerID=b.ConsumerID and b.DeleteFlag=1)
AND ConsumerID=99
Louis Hillebrand
Louis Hillebrand
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1632 Visits: 3349
Using a common_table_expression can make it more readable;


;WITH ToDelete (ConsumerId) AS (
SELECT ConsumerId FROM Table_B WHERE DeleteFlag = 1
EXCEPT
SELECT ConsumerId FROM Table_B WHERE DeleteFlag = 0
)
DELETE TABLE_A
FROM TABLE_A INNER JOIN
ToDelete ON TABLE_A.ComsumerId = ToDelete.ConsumerId


gudukrmca
gudukrmca
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 218
I believe Louis Hillebrand's query deletes all record. Need to add ConsumerID=99 in condition.
RamSteve
RamSteve
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 Visits: 635
here applying condition 99 is not needed as i need to delete all the rows in TableA with the same condition WHERE deleteflag is 1 for all the products
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search