Friday, February 24, 2012

Complex insert?

I am at a loss for how to write an insert for my situation. There are two tables involved:

SubstanceAbuse (OrderId, AssessmentID)
TempAsst(AssessmentID)

The insert has to do this:

For each TempAsst.AssessmentID, insert a record into SubstanceAbuse (3, AssessmentID) if there is not already a record in SubstanceAbuse with that AssessmentID and a value of 3 in the OrderID column.

Thanks!

Maybe something like:

insert into substanceAbuse (orderId, assessmentId)
select distinct
3,
assessmentId
from tempAsst a
where not exists
( select 0 from substanceAbuse b
where a.assessmentId = b.assessmentId
)

|||Perfect! Thank you!!

No comments:

Post a Comment