0

我使用的是 Microsoft SQL Server。我想使用这两个函数来解析进入我的表的数据。所以我同时使用交叉应用和外部应用。

CROSS APPLY CA_Parse_CorpActnDtls_fn(MessageID) ent
outer apply CA_Parse_CorpActnOptnDtls_fn(ev.MessageID) cod

但是当我这样做时,它抱怨以下错误:

违反主键约束“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 构建临时列表?然后我可以更新表格。

任何帮助是极大的赞赏。

4

1 回答 1

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,杰森

于 2017-08-15T20:43:43.343 回答