This is the SQL to pull the records...
Code: Select all
select MD.MASTER_DOCUMENT_ID, MD.MASTER_ID, MD.DOCUMENT_LINK
,D.Document_Id, D.DOCUMENT_TITLE,D.INTERNAL_TITLE, D.REQUIRED_YN
from Document D
left join Master_Document MD on MD.DOCUMENT_ID=D.Document_ID and MD.MASTER_ID=:Master_ID
where (D.Product_Cd=:Product_CD or D.Product_Cd is null)
and D.Document_Domain_Cd=:Document_Domain_CdCode: Select all
INSERT INTO Master_Document
(MASTER_ID, DOCUMENT_ID)
select :Master_ID, :Document_ID
where not exists (select 1 from Master_Document
where Master_ID=:Master_ID and Document_ID=:Document_ID)
if @@rowcount0
set :Master_Document_Id=scope_identity()
UPDATE Master_Document
SET
DOCUMENT_LINK = :DOCUMENT_LINK
WHERE
MASTER_DOCUMENT_ID = :MASTER_DOCUMENT_ID
How can achieve what my update SQL is saying? Maybe an "if exists" test around all insert logic else update.