I was using Microsoft SQL Server. I want to use both functions to parse data going into my table. So I use cross apply and outer apply together.
CROSS APPLY CA_Parse_CorpActnDtls_fn(MessageID) ent
outer apply CA_Parse_CorpActnOptnDtls_fn(ev.MessageID) cod
But when I did this it complain about the following erro:
Violation of PRIMARY KEY constraint 'PK_AfterParse_CA_Events'. Cannot insert duplicate key in object 'dbo.AfterParse_CA_Events'. The duplicate key value is (105818432, 37819929). The statement has been terminated.
The whole T-sql code looks like :
insert into AfterParse_CA_Events (
EventID
,MessageID
,cdtprFunction
,CreationDate
,MsgDefIdr
,EventType
,CFI
,EventProcessingType
,MndtryVlntryEvtTp
,RecordDate
,EffectiveDate
,DueBillRdmDate
,CUSIP
,LSCI_DateOfRecord
,RoundingDesc
)
SELECT ent.EventID
,ent.MessageID
,ent.cdtprFunction
,ent.CreationDate
,ent.MsgDefIdr
,ent.EventType
,ent.CFI
,ent.EventProcessingType
,ent.MndtryVlntryEvtTp
,ent.RecordDate
,ent.EffectiveDate_Cmpny
,ent.DueBillRdmDate
,ent.CUSIP
,ROXSQL.dbo.GetNthTradeDay_fn(
case when ent.EventProcessingType = 'DISN'
then COALESCE (ent.ExDividendDate, ent.RecordDate)
ELSE COALESCE(ent.EffectiveDate_Xchg, ent.EffectiveDate_Cmpny,cod.EarliestPaymentDate_Secu,cod.PaymentDate_Secu ,cod.PaymentDate_Cash)
END,-1) AS LSCI_DateOfRecord
,cod.RoundingDesc
FROM #EventsToDo ev
CROSS APPLY CA_Parse_CorpActnDtls_fn(MessageID) ent
outer apply CA_Parse_CorpActnOptnDtls_fn(ev.MessageID) cod
you can see that I need the second function CA_Parse_CorpActnOptnDtls_fn(ev.MessageID) Because I want to compose an LSCI_DateOfRecord data using my user defined function. so is there any way to avoid the duplicate when I using the two functions together?
or is there any ways to build a temp list for the LSCI_DateOfRecord and RoundingDesc from the second function CA_Parse_CorpActnOptnDtls_fn(ev.MessageID) separately? And then I can update the table.
Any help is greatly appreciated.