我使用的是Microsoft SQL Server。我想使用这两个函数来解析进入我表的数据。所以我使用cross apply和outer apply一起使用。
CROSS APPLY CA_Parse_CorpActnDtls_fn(MessageID) ent
outer apply CA_Parse_CorpActnOptnDtls_fn(ev.MessageID) cod
但是当我这样做时,它会抱怨以下错误:
违反PRIMARY KEY约束' PK_AfterParse_CA_Events'。无法在对象' dbo.AfterParse_CA_Events'中插入重复键。重复键值为(105818432,37819929)。 声明已经终止。
整个T-sql代码如下:
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
你可以看到我需要第二个函数CA_Parse_CorpActnOptnDtls_fn(ev.MessageID) 因为我想使用我的用户定义函数组成一个LSCI_DateOfRecord数据。当我一起使用这两个功能时,有没有办法避免重复?
或者有没有办法分别从第二个函数CA_Parse_CorpActnOptnDtls_fn(ev.MessageID)为LSCI_DateOfRecord和RoundingDesc构建临时列表?然后我可以更新表格。
非常感谢任何帮助。
答案 0 :(得分:0)
查看错误和您的SQL代码,它不是APPLY运算符导致问题本身。事实上,其中一个或两个函数返回的行数超过了一行EvenID& MessageID,这就是造成PK违规的原因。
以下是使用字符串拆分器功能(DelimitedSplit8K)
的简化演示IF OBJECT_ID('tempdb..#EventsToDo ', 'U') IS NOT NULL
DROP TABLE #EventsToDo ;
GO
CREATE TABLE #EventsToDo (
EventID BIGINT NOT NULL,
MessageID BIGINT NOT NULL,
MessageText VARCHAR(1000) NOT NULL
);
GO
INSERT #EventsToDo (EventID, MessageID, MessageText) VALUES
(105818432, 37819929, 'Part 1,Part 2,Part 3,Part 4,Part 5');
GO
-----------------------------------------------------------------
-- create the AfterParse_CA_Events table with PRIMARY KEY (EvenID, MessageID)...
IF OBJECT_ID('tempdb..#AfterParse_CA_Events', 'U') IS NOT NULL
DROP TABLE #AfterParse_CA_Events;
GO
CREATE TABLE #AfterParse_CA_Events (
EvenID BIGINT NOT NULL,
MessageID BIGINT NOT NULL,
MessagePart VARCHAR(1000) NULL
PRIMARY KEY (EvenID, MessageID)
);
GO
--===============================================================
-- see what happens when we try to insert the parsed message values
-- into AfterParse_CA_Events while it has a PK of (EvenID, MessageID)...
INSERT #AfterParse_CA_Events (EvenID, MessageID, MessagePart)
SELECT
etd.EventID,
etd.MessageID,
dsk.Item
FROM
#EventsToDo etd
CROSS APPLY dbo.DelimitedSplit8K(etd.MessageText, ',') dsk;
GO
--===============================================================
-- execute the code below in a separate execution
--===============================================================
-- now, let's modify the AfterParse_CA_Events table so that we have "MessagePartID"
-- and make that part of the PK
IF OBJECT_ID('tempdb..#AfterParse_CA_Events', 'U') IS NOT NULL
DROP TABLE #AfterParse_CA_Events;
GO
CREATE TABLE #AfterParse_CA_Events (
EvenID BIGINT NOT NULL,
MessageID BIGINT NOT NULL,
MessagePartID INT NOT NULL,
MessagePart VARCHAR(1000) NOT NULL
PRIMARY KEY (EvenID, MessageID, MessagePartID)
);
GO
--===============================================================
-- Now let's try the insertion again...
INSERT #AfterParse_CA_Events (EvenID, MessageID, MessagePartID, MessagePart)
SELECT
etd.EventID,
etd.MessageID,
dsk.ItemNumber,
dsk.Item
FROM
#EventsToDo etd
CROSS APPLY dbo.DelimitedSplit8K(etd.MessageText, ',') dsk;
GO
--===============================================================
-- check the inserted values...
SELECT
*
FROM
#AfterParse_CA_Events apce;
HTH,Jason