Hi I have created a sql query as seen below. Basically I need to achieve two things. 1. Insert record only if the correlationid doesnt exist in correlationmessage table. 2. Insert two records one with MessageTypeId = 1 and MessageTypeId = 2.
I have tried and achieved the second one but dont know how to do the first one
insert into MESSAGING.CorrelationMessage
(CorrelationId, MessageTypeId, RequestMessage,ResponseMessage, IsMatched, CreatedDate)
(
Select ActivityID, 1 as MessageTypeId, NULL as RequestMessage, NULL as ResponseMessage, 1 as IsMatched, GETDATE() as CreatedDate
from account.Activity a
join account.ActivityType b on b.ActivityTypeID=a.ActivityTypeID
join ACCOUNT.TransactionType c on c.TransactionTypeID=a.TransactionTypeID
where a.ActivityTypeID=3
and ExecutingBroker is null
and a.active=1
and TradeDate is null
and DateforGTDOrders > GETDATE()
union
Select ActivityID, 2 as MessageTypeId, NULL as RequestMessage, NULL as ResponseMessage, 1 as IsMatched, GETDATE() as CreatedDate
from account.Activity a
join account.ActivityType b on b.ActivityTypeID=a.ActivityTypeID
join ACCOUNT.TransactionType c on c.TransactionTypeID=a.TransactionTypeID
where a.ActivityTypeID=3
and ExecutingBroker is null
and a.active=1
and TradeDate is null
and DateforGTDOrders > GETDATE()
)
This query is a bit different as it to doing multiple inserts based on select as also checking if the record exists. Since it is a union it is quite tricky to do the check.