Solved

How to delete records that not exist in TABLEB

Posted on 2013-01-13
3
352 Views
Last Modified: 2013-01-14
Hi!

Have two tables -> TABLEA and TABLEB

I want to check if condision TABLEA.ITEMID exist in TABLEB.ITEMID
If TABLEA.ITEMID dosent exist in TABLEB.ITEMID
it must delete the record from TABLEA

How can i do this ?
0
Comment
Question by:team2005
[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
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 38772941
DELETE FROM TABLEA
FROM TABLEA LEFT JOIN
    TABLEB ON TABLEA.ITEMID = TABLEB.ITEMID
WHERE TABLEB.ITEMID IS NULL
0
 
LVL 42

Assisted Solution

by:pcelba
pcelba earned 250 total points
ID: 38772958
I would recommend slower (obviously) but better readable syntax:

DELETE FROM TABLEA
  WHERE ITEMID NOT IN (SELECT ITEMID FROM TABLEB)
0
 
LVL 2

Author Closing Comment

by:team2005
ID: 38773532
Thanks
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

690 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