Question : IF EXISTS, OUTPUT INTO

SELECT firstfield,computedvalue
FROM tableA
WHERE.....
EXCEPT
SELECT firstfield,computedvalue
FROM tableB
WHERE.....

Right now, I am inserting  firstfield,computedvalue into a #temp table.  IF @@rowcount > 0, I am then returning to the front end any data from tableA that does not exist in tableB.  
Note, I am not returning all values for the relevant records... just a few identifying values, so they have a quick subset of the data to review.

IF EXISTS on the EXCEPT, I still need to return a little subset of the records to the front end for review, but I also need to insert ALL values for the relevant records into a Working table.  After the user views the data at the front end, he/she will then execute an INSERT procedure, writing the data from the working table into the target table.  In this example, that would be 'tableB'.

IF EXISTS(
SELECT firstfield,computedvalue
FROM tableA
WHERE.....
EXCEPT
SELECT firstfield,computedvalue
FROM tableB
WHERE.....)


Basically, I need a little direction on the ideal way to redirect my OUTPUT INTO.
The EXCEPT is only based on two fields, one of which is computed.  IF EXISTS, I need to return a few fields to the front end for review AND insert the entire record(s) into a working table.

Please advise.

Answer : IF EXISTS, OUTPUT INTO

ok, in such case here is your solution

within this one query you are selecting all the records that interest you, inset full rows from tableA to workingtable and return two columns to the user
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
insert into workTable
output inserted.firstfield, inserted.computedValue
select OriginalTable.* 
from (
	SELECT firstfield,computedvalue
	FROM tableA
	WHERE.....
	EXCEPT
	SELECT firstfield,computedvalue
	FROM tableB
	WHERE.....
) WantedRows
inner join tableA OriginalTable 
	on WantedRows.computedValue = OriginalTable.computedvalue
	
Random Solutions  
 
programming4us programming4us