1

我正在尝试创建一个存储过程(MS SQL Server 2012),它复制许多行,然后将它们插入到同一个表中,并关联到另一个外部表。在复制的行上,我们还更改了 updateddate 和 createddate,以及创建和更新这些新插入行的用户。

这一切都很好,直到我们来到主键。该数据库是 Sage CRM 的数据库——它使用存储过程 (eware_get_identity_id) 来获取我们要插入的表的下一个主键 ID。这个 eware_get_identity_id 存储过程对其他表进行各种插入、更新和删除,所以我们不能使用函数来代替。我们无法更改数据库的组成,因此将列的类型更改为身份规范也不是一种选择。

我想做的(我知道我做不到)是这样的:

ALTER PROCEDURE Lead_CopySecuritiesToOpp
    @lead_leadID int,
    @oppo_opportunityID int,
    @user_userID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    INSERT INTO Security
            (<columns hidden for clarity>,secu_CreatedDate,secu_UpdatedDate,secu_CreatedBy,secu_UpdatedBy, 
                         secu_LeadId, secu_CaseId,secu_SecurityID)
    SELECT  <columns hidden for clarity>, GETDATE() as secu_CreatedDate, GETDATE() as secu_UpdatedDate, @user_userID as secu_CreatedBy, 
             @user_userID as secu_UpdatedBy, NULL as secu_LeadId, @oppo_opportunityID as secu_CaseId,
             (exec [dbo].[eware_get_identity_id] 'Security') as secu_SecurityID
        FROM            Security
        WHERE        (secu_LeadId = @lead_leadID)

END
GO

显然我不能以这种方式内联执行存储过程((exec [dbo].[eware_get_identity_id] 'Security') as secu_SecurityID)。如果 Sage SP 不更改数据,则函数将是理想的,但它确实更改了数据,因此这不是一个选项。任何人都可以提出更好的方法吗?我确实考虑过在插入之前使用游标或临时表来获取数据?请提出建议。

4

1 回答 1

0

我最终使用游标为每一行执行 SP。一次只会执行一两行 - 所以这个解决方案有效(即使它不是很漂亮)。

ALTER PROCEDURE Lead_CopySecuritiesToOppo
    -- Add the parameters for the stored procedure here
    @lead_leadID int,
    @oppo_opportunityID int,
    @user_userID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    declare @secu_SecurityID char( 11 )
    declare @newsecu_SecurityID int

    set rowcount 0
    select * into #mytemp from Security where secu_LeadId = @lead_leadID

    set rowcount 1

    select @secu_SecurityID = secu_SecurityID from #mytemp

    --Cursor through all the returned security rows
    while @@rowcount <> 0
    begin
        set rowcount 0

        --Get the next ID
        EXEC @newsecu_SecurityID = [dbo].[eware_get_identity_id] 'Security'

        --Insert this row into the security table
        INSERT INTO Security
                        (secu_TimeStamp, secu_Deleted, secu_Secterr, secu_Name, 
                         secu_Status, secu_propType, secu_typeN, secu_noBeds, secu_noFloors, secu_noFlats, secu_purpBuilt, secu_stanConst, 
                         secu_stanConstN, secu_yearBuilt, secu_tenure, secu_leaseLeft, secu_exLEA, secu_exLEAN, secu_adjComP, secu_adjComPN, secu_letToFam, 
                         secu_letToFamN, secu_tenType, secu_purPrice, secu_purPrice_CID, secu_purchasedDate, secu_estValue, secu_estValue_CID, secu_reinstval, 
                         secu_reinstval_CID, secu_rentInc, secu_rentInc_CID, secu_valCont, secu_valTel, secu_loanType, secu_address1, secu_address2, secu_address3, 
                         secu_address4, secu_postcode, secu_primary,secu_CreatedDate,secu_UpdatedDate,secu_CreatedBy,secu_UpdatedBy, 
                             secu_LeadId, secu_CaseId,secu_SecurityID)
        SELECT              secu_TimeStamp, secu_Deleted, secu_Secterr, secu_Name, 
                             secu_Status, secu_propType, secu_typeN, secu_noBeds, secu_noFloors, secu_noFlats, secu_purpBuilt, secu_stanConst, 
                             secu_stanConstN, secu_yearBuilt, secu_tenure, secu_leaseLeft, secu_exLEA, secu_exLEAN, secu_adjComP, secu_adjComPN, secu_letToFam, 
                             secu_letToFamN, secu_tenType, secu_purPrice, secu_purPrice_CID, secu_purchasedDate, secu_estValue, secu_estValue_CID, secu_reinstval, 
                             secu_reinstval_CID, secu_rentInc, secu_rentInc_CID, secu_valCont, secu_valTel, secu_loanType, secu_address1, secu_address2, secu_address3, 
                             secu_address4, secu_postcode, secu_primary, GETDATE() as secu_CreatedDate, GETDATE() as secu_UpdatedDate, @user_userID as secu_CreatedBy, 
                             @user_userID as secu_UpdatedBy, NULL as secu_LeadId, @oppo_opportunityID as secu_CaseId, @newsecu_SecurityID as secu_SecurityID
        FROM            #mytemp where secu_SecurityID = @secu_SecurityID

        delete #mytemp where secu_SecurityID = @secu_SecurityID

        set rowcount 1
        select @secu_SecurityID = secu_SecurityID from #mytemp
    end
    set rowcount 0

END
GO
于 2013-08-29T11:05:09.787 回答