-2

I'm trying to write a query where if data already exist in one table I don't want it to be returned in this query.

I hope that makes sense, I don't really know how to word it.

4
  • 1
    what? what ? you are asking ?
    – vhadalgi
    Dec 6, 2013 at 11:29
  • You should add table schema to the question. Like this you are making it harder to answer a simple question Dec 6, 2013 at 11:30
  • no! it is not making sense. Dec 6, 2013 at 11:32
  • Chill your beans, I'm new to all this, new job, in training etc
    – Ryan
    Dec 6, 2013 at 11:58

4 Answers 4

3

use NOT EXISTS:

IF NOT EXISTS(SELECT * FROM TABLETOCHECK)
BEGIN
   SELECT * FROM TABLE
END
1

I normally use

Columnfromonetable IS NOT IN (SELECT Column from yourtable)

For example if you were checking ID's between 2 tables like CustomerID, productID etc

0

Use the DISTINCT column names it will not return the duplicate column values.

0

Return all records in TableA that are not in TableB based on a field called ID

SELECT a.* FROM TableA a 
Left join TableB b ON a.ID = b.ID
WHERE b.ID IS NULL

Not the answer you're looking for? Browse other questions tagged or ask your own question.