从存储过程中的插入调用存储过程

时间:2013-08-28 21:51:57

标签: sql-server stored-procedures

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

这一切都很好,直到我们来到主键。该数据库是Sage CRM的数据库 - 它使用存储过程(eware_get_identity_id)来获取我们插入的表的下一个主键ID。这个eware_get_identity_id存储过程会对其他表执行各种插入,更新和删除操作,因此我们不能使用函数。我们无法更改数据库的组成,因此也不能将列的类型更改为Identity规范。

我想做什么(我知道我做不到)就像下面这样:

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没有改变数据,那么函数将是理想的,但它确实如此,因此这不是一个选项。谁能建议更好的方法呢?我确实考虑过使用游标,或者可能是临时表来在插入之前获取数据?建议请。

1 个答案:

答案 0 :(得分: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