views:

790

answers:

9

Its not really a subtraction I'm looking for. And I know its not a union or intersection... I have been given a long and complex stored procedure that returns a table of active and inactive documents. I have also been given a similar stored procedure that returns another table that contains only the active documents.

How could I get a table of inactive documents using these two store procedures?

We are using SQL Server 2005.

+3  A: 
select * from MyTable1
where MyTable1.Field1 not in (
  select Field1 from MyTable2)
tsilb
+6  A: 

Assuming there are unique IDs that correspond across the two tables:

select * from table_both b
where not exists (select * from table_active a where a.id = b.id)
Carl Manaster
+8  A: 

The set operation you are looking for is called MINUS, but in SQL Server the keyword is EXCEPT

  SELECT ... // all documents
  EXCEPT
  SELECT ... // active documents

I believe that the EXCEPT set operation became available in SQL Server 2005.

LBushkin
This is the right answer with respect to the underlying set operation.
Michael van der Westhuizen
Does that exist on Sql Server ?
iDevlop
Not in SQL Server. "It depends"
gbn
In SQL Server, the equivalent of MINUS is called EXCEPT
LBushkin
+3  A: 

I believe EXCEPT is what you are looking for. Syntax is similar to UNION or INTERSECT.

Dave Pirotte
+2  A: 

What's your DB engine?

In Oracle, you could use MINUS set operation.

In MS SQLServer 2005 and newer you can use EXCEPT.

Pablo Santa Cruz
+1  A: 

In MS TSql, I think you want the EXCEPT keyword.

query1 EXCEPT query2

Which will return all rows found in the first query that are not also found in the second query.

Matthew Jones
+1  A: 
SELECT * FROM Table1 
LEFT JOIN Table2 on Table1.id = Table2.id
WHERE Table2.id IS NULL

this should work on almost any database engine

Jason S
+2  A: 
SELECT both.*
FROM both LEFT OTUER JOIN inactives USING (whatever_id)
WHERE inactives.whatever_id IS NULL;

or

SELECT * FROM both
EXCEPT
SELECT * FROM inactives;
Michael Krelin - hacker
+5  A: 

All good answers, but missing one point: The questioner (OP) has stored procedures...

You have to define temporary tables (based on your platform) to load the data

INSERT ...
EXEC getActive

INSERT ...
EXEC getInactive

Then use EXCEPT/EXISTS/MINUS/IN/OUTER JOIN/etc...

gbn
what means OP ?
iDevlop
Sorry: "original poster"
gbn