无法在对象'dbo.AoObject'中插入具有唯一索引'AoObject_AK_ClassName'的重复键行

时间:2014-04-29 13:49:03

标签: sql sql-server sql-server-2008 stored-procedures

这是我的存储过程,有人可以帮我解决这个错误的原因吗?

错误:System.Data.SqlClient.SqlException:无法在对象'dbo.AoObject'中插入具有唯一索引'AoObject_AK_ClassName'的重复键行。无法在具有唯一索引'AoObject_AK_ClassName'的对象'dbo.AoObject'中插入重复键行

Text


CREATE Procedure [dbo].[APX2_TRX_CreatePortfolio](@TrxPortfolioID int) 
AS 
BEGIN 

declare @XmlRelationshipDocp as nvarchar(2000) 
declare @TradexPortfolioCode as nvarchar(100) 
declare @ContactID as int 
declare @TRX_CUSTOMER_NAME as nvarchar(255) 
declare @APXUserGroupID as int 
declare @P1 as int 
declare @TrxCustomerID as int 
declare @ApxPortFolioID as int 
declare @AxysPortfolioID as nvarchar(50) 
declare @PrimaryContactID as int 
declare @DbAction as nvarchar(2) 
declare @StartDate as nvarchar(15) 
declare @XmlPortfolioDocp nvarchar(3999) 
declare @TRX_PORTFOLIO_EXTERNALNUMERIC_ID as int 
declare @XmlPortfolioGroupAssociationDocp nvarchar(3999) 
declare @flatfee as nvarchar(72) 
declare @investortype as nvarchar(72) 
declare @agreementtype as nvarchar(72) 
declare @forvalter as nvarchar(72) 
declare @ownedby as nvarchar(20) 
declare @CRMID as nvarchar(30) 
DECLARE @Country as nvarchar(20) 
declare @ApxGroupConst as nvarchar(50) 
declare @PortfolioGroupID as nvarchar(50) 
declare @ProcessingGroupID as nvarchar(50) 

SET @ApxGroupConst= 'Default Owners' 
SET @PortfolioGroupID = (Select convert(nvarchar, ObjectID)  
                                                 From ApxFirm..AOObject  
                                                 where name like 'master'  
                                                 and ClassID = (Select ClassID  
                                                                                From ApxFirm..AOClass  
                                                                                Where ClassDescription = 'Portfolio Group') 
                                                                                ) 
SET @ProcessingGroupID = '-14' 

SELECT @Country=GO_COUNTRY_CODE 
FROM GO_COUNTRY,GO_DEPARTMENT,TRX_CUSTOMER,TRX_PORTFOLIO,GO_USER 
WHERE 1=1 
AND TRX_PORTFOLIO.TRX_CUSTOMER_ID = TRX_CUSTOMER.TRX_CUSTOMER_ID 
AND TRX_CUSTOMER.GO_USER_ID = GO_USER.GO_USER_ID 
AND GO_USER.GO_DEPARTMENT_ID = GO_DEPARTMENT.GO_DEPARTMENT_ID 
AND GO_DEPARTMENT.GO_COUNTRY_ID = GO_COUNTRY.GO_COUNTRY_ID 
AND TRX_PORTFOLIO.TRX_PORTFOLIO_ID = @TrxPortfolioID 

SELECT @TradexPortfolioCode = TRX_PORTFOLIO_NAME 
,@ContactID = convert(int, TRX_CUSTOMER_EXTERNALID) 
,@TRX_CUSTOMER_NAME = REPLACE(RTRIM(LTRIM(LEFT(REPLACE(REPLACE(ISNULL(TRX_CUSTOMER_NAME,''),'&',''),',',''),50))),char(13),'') 
,@TrxCustomerID = TRX_CUSTOMER.TRX_CUSTOMER_ID 
,@AxysPortfolioID = AXYS_PORTFOLIO_ID 
,@TRX_PORTFOLIO_EXTERNALNUMERIC_ID = TRX_PORTFOLIO_EXTERNALNUMERIC_ID 
,@StartDate = convert(nvarchar,year(getdate())) + '-' + convert(nvarchar,month(getdate())) + '-' + convert(nvarchar,day(getdate())) 
FROM TRX_PORTFOLIO, TRX_CUSTOMER 
WHERE TRX_PORTFOLIO.TRX_CUSTOMER_ID = TRX_CUSTOMER.TRX_CUSTOMER_ID 
AND TRX_PORTFOLIO_ID = @TrxPortfolioID 
AND ISNUMERIC(TRX_CUSTOMER_EXTERNALID) = 1 

SET @PrimaryContactID = @ContactID 

select @CRMID = CRM_ID 
from trx_customer where trx_Customer_id = @TrxCustomerID 

--IF ISNUMERIC(LEFT(@CRMID, 1)) = 1 SET @ownedby = 'Nominee' 
--ELSE 
--BEGIN 
--    IF LEFT(@CRMID, 1) = 'D' SET @ownedby = 'FH' 
--    ELSE 
--    IF LEFT(@CRMID, 2) = 'BP' SET @ownedby = 'BP' 
--    ELSE  
--    SET @ownedby = 'FFEkstra' 
--END 

SET @ownedby = @TRX_CUSTOMER_NAME 

if @TRX_PORTFOLIO_EXTERNALNUMERIC_ID IS NULL 

BEGIN 

       SELECT @TRX_PORTFOLIO_EXTERNALNUMERIC_ID = portfolioId 
       FROM ApxFirm.advapp.vPortfolio 
       WHERE OwnerContactID  = @contactId 
       AND PortfolioCode  COLLATE SQL_Latin1_general_CP1_CI_AS = @TradexPortfolioCode  COLLATE SQL_Latin1_general_CP1_CI_AS 



       IF @TRX_PORTFOLIO_EXTERNALNUMERIC_ID IS NOT  NULL 

       BEGIN 

             UPDATE TRX_PORTFOLIO 

                SET TRX_PORTFOLIO_EXTERNALNUMERIC_ID= @TRX_PORTFOLIO_EXTERNALNUMERIC_ID 

             WHERE TRX_PORTFOLIO_ID = @TrxPortfolioID 

       END 

       SET @P1 = @TRX_PORTFOLIO_EXTERNALNUMERIC_ID 

END 



print 'Current contactID ' + convert(nvarchar, ISNULL(@ContactID,0)) 
if @ContactID IS NOT NULL ANd @TRX_PORTFOLIO_EXTERNALNUMERIC_ID IS NULL AND (@TradexPortfolioCode IS NOT NULL AND @StartDate IS NOT NULL ) 
BEGIN 
print 'inside insert' 
                SELECT @APXUserGroupID = UserGroupID  
                FROM ApxFirm..vQbRowDefUserGroup  
                WHERE UserGroupName like @ApxGroupConst 

                SET @DbAction = 'I' 
                set @P1=NULL 
        --'ReportHeading="' + @TRX_CUSTOMER_PERSONALID  +  '" 
      Declare @Joint_IDs Varchar(1024) 
      SELECT @Joint_IDs =REPLACE(isnull(TRX_PORTFOLIO_CUSTOMFIELD2,''),'&','') from trx.dbo.trx_portfolio where  trx_portfolio_ID = @TrxPortfolioID 

 --@TradexPortfolioCode ,,@ContactID,@StartDate  

        SELECT @XmlPortfolioDocp = '<?xml version="1.0" encoding="UTF-16" ?> 
                <Root> 
                <Portfolio ShortName="' + @TradexPortfolioCode + '" ReportHeading1="' + 
                 ISNULL(@TRX_CUSTOMER_NAME,'Default trx') + '" ReportHeading2="' + 
                 ISNULL(@Joint_IDs,'') + '" OwnerName="' +isnull(@ownedby,'Nominee') + 
                '" PortfolioCode="' + @TradexPortfolioCode + '" OwnerContactID="' + convert(nvarchar,@ContactID) +  
                '" PortfolioTypeCode="Fund Shareholder" PrimaryContactID="' + convert(nvarchar,@ContactID) +  
                '"  OwnerAddressLabel="Home" OwnerID="' + convert(nvarchar(10),@APXUserGroupID) +  
                '" OwnerPhoneLabel="Home" ProcessingGroupID="' + @ProcessingGroupID + '" Adder="0"  DBAction="I" StartDate="' + @StartDate +  
                ' 00:00:00.000"> 
                <_SysCriteriaRowExclude>false</_SysCriteriaRowExclude> 
                </Portfolio> 
                </Root>' 

print @XmlPortfolioDocp  

                SELECT @XmlRelationshipDocp = '<?xml version="1.0" encoding="UTF-16" ?> 
                <Root> 
                <ContactRelationship ContactID="' + convert(nvarchar,@ContactID) + '" PortfolioInheritance="None"  
                RelatedContactID="' + convert(nvarchar,@ContactID) + '" RelationshipType="5" DBAction="M"> 
                <_SysCriteriaRowExclude>false</_SysCriteriaRowExclude> 
                <InterestedParty PortfolioCode="' + @TradexPortfolioCode + '" OwnerContactID="' + convert(nvarchar,@ContactID) + 
                 '" RelatedContactID="' + convert(nvarchar,@ContactID) + '" RelationshipType="5" DBAction="M"> 
                <_SysCriteriaRowExclude>false</_SysCriteriaRowExclude> 
                </InterestedParty> 
                </ContactRelationship> 
                </Root>' 


                exec ApxFirm..pQbTxProcessPortfolio @ParamPortfolioID = @P1 output 
                , @AuthUserID = -1001 
                , @RunAsUserID = -1001 
                , @IsSQLTransON = 1 
                , @XmlPortfolioDoc = @XmlPortfolioDocp  
                , @XmlPortfolioBaseLabelDoc = default 
                , @XmlPortfolioBaseAccessRightsDoc = default 
                , @XmlPortfolioBaseReinvestExceptionDoc = default 
                , @XmlPortfolioBaseIndexDoc = default 
                , @XmlPortfolioBaseSyntheticIndexDoc = default 
                , @XmlPortfolioAmortizationDoc = default 
                , @XmlPortfolioGroupAssociationDoc = default     
                , @XmlPortfolioCompositeAssociationDoc = default 
                , @XmlInterestedPartyDoc = default--@XmlRelationshipDocp  
                , @XmlOwnerContactDoc = default 
                , @XmlBillingContactDoc = default 
                , @XmlBankContactDoc = default 



                SET @ApxPortFolioID = @P1 

                UPDATE TRX_PORTFOLIO  
                SET TRX_PORTFOLIO_EXTERNALNUMERIC_ID = @P1 
                WHERE TRX_PORTFOLIO_ID = @TrxPortfolioID 


                SET @XmlPortfolioDocp = '<?xml version="1.0" encoding="UTF-16" ?> 
                <Root> 
                <Portfolio BaseExtPortfolioBaseID="' + convert(nvarchar,@P1) + '" PortfolioCode="' + @TradexPortfolioCode + 
                 '" RowVersion="0" PortfolioID="' + convert(nvarchar,@P1) + '" DBAction="U"> 
                <_SysCriteriaRowExclude>false</_SysCriteriaRowExclude> 
                </Portfolio> 
                </Root>' 


                SELECT @XmlPortfolioGroupAssociationDocp = '<?xml version="1.0" encoding="UTF-16" ?> 
                <Root> 
                <PortfolioGroupAssociation OwnerName="Nominee" ReportHeading="Holding" MemberID="' + convert(nvarchar,@P1) + 
                 '" PortfolioGroupID="' + @PortfolioGroupID + '" MemberCode="' + @TradexPortfolioCode + 
                 '" PortfolioGroupCode="tradex" DBAction="I" CanUpdate="1" IsConsolidated="0" IsDirectMember="1"> 
                <_SysCriteriaRowExclude>false</_SysCriteriaRowExclude> 
                </PortfolioGroupAssociation> 
                </Root>' 

                exec ApxFirm..pQbTxProcessPortfolio @ParamPortfolioID = @P1 output 
                , @AuthUserID = -1001 
                , @RunAsUserID = -1001 
                , @IsSQLTransON = 1 
                , @XmlPortfolioDoc = @XmlPortfolioDocp 
                , @XmlPortfolioBaseLabelDoc = default 
                , @XmlPortfolioBaseAccessRightsDoc = default 
                , @XmlPortfolioBaseReinvestExceptionDoc = default 
                , @XmlPortfolioBaseIndexDoc = default 
                , @XmlPortfolioBaseSyntheticIndexDoc = default 
                , @XmlPortfolioAmortizationDoc = default 
                , @XmlPortfolioGroupAssociationDoc = @XmlPortfolioGroupAssociationDocp 
                , @XmlPortfolioCompositeAssociationDoc = default 
                , @XmlInterestedPartyDoc = default 
                , @XmlOwnerContactDoc = default 
                , @XmlBillingContactDoc = default 
                , @XmlBankContactDoc = default 





END 
ELSE 
BEGIN 


      Declare @Joint_IDs2 Varchar(1024) 
      SELECT @Joint_IDs2 =REPLACE(isnull(TRX_PORTFOLIO_CUSTOMFIELD2,''),'&','') from trx.dbo.trx_portfolio where  trx_portfolio_ID = @TrxPortfolioID 


   -- SULTAN incase of the update  
declare @p11 int, @APX_PortfolioID INT 
set @p11=NULL 
declare @p5 XML 

SELECT @APX_PortfolioID = [PortfolioID] FROM [APXFirm].[dbo].[AdvPortfolio] WHERE [ShortName] = @TradexPortfolioCode 
PRINT '@APX_PortfolioID' 
PRINT @APX_PortfolioID 
IF (not @APX_PortfolioID is null) 
BEGIN 
PRINT 'DDAAAAA' 
SET @p5 = CONVERT(XML, N'<Root><Portfolio ShortName="' + CONVERT(VARCHAR (50), @TradexPortfolioCode) +  
'" ReportHeading2="' + ISNULL(@Joint_IDs2,'Default trx') + 
'" PortfolioID="' + CONVERT(VARCHAR (50),@APX_PortfolioID )+  
'" BaseExtPortfolioBaseID="' +  CONVERT(VARCHAR (50),@APX_PortfolioID) +  
'" RowVersion="3" DBAction="U"><_SysPrimaryKey>1</_SysPrimaryKey></Portfolio> </Root>') 

PRINT 'DDDD' 
PRINT CONVERT(VARCHAR(1004),@p5) 
PRINT 'DDDD2' 
 exec [APXFirm]..pQbTxProcessPortfolio @ParamPortfolioID=@p11 output,@AuthUserID=-1001,@RunAsUserID=-1001,@IsSQLTransON=1,@XmlPortfolioDoc=@p5,@XmlPortfolioBaseLabelDoc=default,@XmlPortfolioBaseAccessRightsDoc=default,@XmlPortfolioBaseReinvestExcepti
onDoc=default,@XmlPortfolioBaseIndexDoc=default,@XmlPortfolioBaseSyntheticIndexDoc=default,@XmlPortfolioAmortizationDoc=default,@XmlPortfolioGroupAssociationDoc=default,@XmlPortfolioCompositeAssociationDoc=default,@XmlInterestedPartyDoc=default,@XmlOwnerC
ontactDoc=default,@XmlBillingContactDoc=default,@XmlBankContactDoc=default 
PRINT 'DDDD3' 

END 

END 

if @P1 IS NULL AND @TRX_PORTFOLIO_EXTERNALNUMERIC_ID IS NOT NULL 
BEGIN 
        SELECT @P1 = @TRX_PORTFOLIO_EXTERNALNUMERIC_ID 
END 

BEGIN TRANSACTION 

exec ApxFirm.dbo.pAdvAuditEventBegin -1001 
if @P1 IS NOT NULL ANd @ContactID IS NOT NULL 
BEGIN 
        IF NOT EXISTS (SELECT * FROM ApxFirm.APX.InterestedParty WHERE PortfolioID = @P1 AND ContactID = @ContactID) 
        BEGIN 
                INSERT INTO ApxFirm.APX.InterestedParty(PortfolioID, ContactID) 
                VALUES(@P1, @ContactID) 
        END 

        IF NOT EXISTS (SELECT * FROM ApxFirm.APX.ResponsibleParty WHERE PortfolioID = @P1 AND ContactID = @ContactID AND ResponsibleTypeID = 1) 
        BEGIN 
                INSERT INTO ApxFirm.APX.ResponsibleParty (PortfolioID, ResponsibleTypeID, ContactID, ContactIDOrZero) 
                VALUES(@P1 , 1, @ContactID, 0) 
        END 

        IF NOT EXISTS (SELECT * FROM ApxFirm.APX.ResponsibleParty WHERE PortfolioID = @P1 AND ContactID = @ContactID AND ResponsibleTypeID = 4) 
        BEGIN 
                INSERT INTO ApxFirm.APX.ResponsibleParty (PortfolioID, ResponsibleTypeID, ContactID, ContactIDOrZero) 
                VALUES(@P1 , 4, @ContactID, 0) 
        END 

        --Set report heading1 
        UPDATE ApxFirm..AoObject 
        SET [DisplayName] = (SELECT LEFT(ISNULL(TRX_CUSTOMER_NAME,''),71) FROM TRX_CUSTOMER WHERE TRX_CUSTOMER_ID=@TrxCustomerID) 
        ,[NAME] = @TradexPortfolioCode 
        WHERE ObjectID = @P1 

        IF @Country = 'SE' 
        BEGIN 
        update ApxFirm..advportfoliosetting 
        set closingmethodcode = 'a' 
        where PortfolioSettingID = @P1 

        update ApxFirm..advportfoliobase 
        set ReportingCurrencyCode = 'se' 
        where PortfolioBaseID = @P1 
END 
END 


 if @@ERROR = 0  
   BEGIN      
COMMIT 
END  
ELSE 
BEGIN 
ROLLBACK 
END 

END 

     /* datatable executor tradex class:ReportPaintGet.xGet */

我现在还不知道从哪里开始搜索, 我很困惑

2 个答案:

答案 0 :(得分:0)

如错误所示,您尝试在表dbo.AoObject中插入重复值,该表在某些列上具有唯一约束'AoObject_AK_ClassName'(根据命名约束命名约定,可能是ClassName)。

解决方案可能是检查数据/查询重复值的来源和正确的数据或查询。

没有数据&amp;实际数据库,实际上可能很难实现。

我建议调试如下:

  • 首先选择将数据放入变量和放大器的位置。选择/打印数据/变量&amp;仅返回该步骤。
  • 以这种方式,向下再增加一个查询&amp;返回/选择每个步骤的数据&amp;查看您获取重复数据/现有类名的位置(存在唯一约束的列)。

答案 1 :(得分:0)

不确定您发布此SQL的原因,没有INSERT语句。您需要知道错误的来源并知道您拥有的数据。 INSERT重复的KEY错误在含义上非常明确。在测试之前知道你在插入并知道它是否已经存在。