2012-01-10 114 views
1

因此,我正在使用遊標循環查看返回的一堆記錄。我剛更新了表中的一些細節,現在我想從該表中提取細節,因此我使用了臨時表。將表格變量插入到具有附加值的其他表格中

所以現在我想一些值插入到一個新表是無關的最後一個,然後將其餘值會從表變量直接拷貝...我怎樣才能做到這一點?

我會後有問題的部分下方,以幫助人們看到什麼,我試圖做的。 有問題的部分位於更新狀態評論和上述未完成評論之間。

OPEN cur 
FETCH NEXT FROM cur INTO @MembershipTermID , @EndDate , @MembershipID <VARIABLES> 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    --PERFORM ACTION 
    DECLARE @TodaysDate DATETIME 
    SET @TodaysDate = getDate() 

    --CANCEL DETAIL 
    DECLARE @CancellationDetailID INT 
    INSERT INTO CancellationDetail(CancellationDetailID,RefundAmount,OldEndDate,EffectiveDate,CancelDate,ReasonCodeProgKey) 
    VALUES (0, 0.0, @EndDate, @TodaysDate, @TodaysDate, 'CANC_DORMANT') 
    SELECT @CancellationDetailID = SCOPE_IDENTITY()  
    INSERT INTO CancellationDetailAudit(StampUser,StampDateTime,StampAction,CancellationDetailID,RefundAmount,OldEndDate,EffectiveDate,CancelDate,ReasonCodeProgKey) 
    VALUES('SYSTEM', GetDate(), 'I', @CancellationDetailID, 0.0, @EndDate, @TodaysDate, @TodaysDate, 'CANC_DORMANT') 

    --LINK TO TERM 
    INSERT INTO MembershipTermCancellationDetail(CancellationDetailID,MembershipTermID) 
    VALUES(@CancellationDetailID, @MembershipTermID) 
    INSERT INTO MembershipTermCancellationDetailAudit(StampUser,StampDateTime,StampAction,MembershipTermCancellationDetailID,CancellationDetailID,MembershipTermID) 
    VALUES('SYSTEM', GetDate(), 'I', 0, @CancellationDetailID, @MembershipTermID) 

    --UPDATE STATUS 
    UPDATE MembershipTerm 
    SET MemberStatusProgKey = 'CANCELLED', 
    EndDate = @TodaysDate, 
    UpdateDateTime = @TodaysDate, 
    AgentID = 224, 
    NextTermPrePaid = 'False' 
    WHERE MembershipTermID = @MembershipTermID 

    DECLARE @MembershipTermTable TABLE 
    (
    MembershipTermID int, 
    MemberStatusProgKey nvarchar (50), 
    StartDate datetime, 
    EndDate datetime, 
    AdditionalDiscount float, 
    EntryDateTime datetime, 
    UpdateDateTime datetime, 
    MembershipID int, 
    AgentID smallint, 
    PlanVersionID int, 
    ForceThroughReference nvarchar (255), 
    IsForceThrough bit, 
    NextTermPrePaid bit, 
    IsBillingMonthly bit, 
    LastPaymentDate datetime, 
    PaidToDate datetime, 
    IsIndeterminate bit 
    ) 

    INSERT INTO @MembershipTermTable 
    SELECT MembershipTermID, 
    MemberStatusProgKey, 
    StartDate, 
    EndDate, 
    AdditionalDiscount, 
    EntryDateTime, 
    UpdateDateTime, 
    MembershipID, 
    AgentID, 
    PlanVersionID, 
    ForceThroughReference, 
    IsForceThrough, 
    NextTermPrePaid, 
    IsBillingMonthly, 
    LastPaymentDate, 
    PaidToDate, 
    IsIndeterminate 
    FROM MembershipTerm 
    WHERE MembershipTermID = @MembershipTermID 

    INSERT INTO MembershipTermAudit(StampUser,StampDateTime,StampAction,MembershipTermID,MemberStatusProgKey,StartDate,EndDate,AdditionalDiscount,EntryDateTime,UpdateDateTime,MembershipID,AgentID,PlanVersionID,ForceThroughReference,IsForceThrough,NextTermPrePaid,IsBillingMonthly,LastPaymentDate,PaidToDate,IsIndeterminate) 
    VALUES ('SYSTEM',@TodaysDate,'I',MembershipTermID,MemberStatusProgKey,StartDate,EndDate,AdditionalDiscount,EntryDateTime,UpdateDateTime,MembershipID,AgentID,PlanVersionID,ForceThroughReference,IsForceThrough,NextTermPrePaid,IsBillingMonthly,LastPaymentDate,PaidToDate,IsIndeterminate) 
    --ABOVE NOT FINISHED, NEED TO ADD AUDIT RECORD CORRECTLY 

    --Members 
    DECLARE @MembersTable TABLE 
    (
    MembershipTermID int, 
    MemberStatusProgKey nvarchar (50), 
    StartDate datetime, 
    EndDate datetime, 
    AdditionalDiscount float, 
    EntryDateTime datetime, 
    UpdateDateTime datetime, 
    MembershipID int, 
    AgentID smallint, 
    PlanVersionID int, 
    ForceThroughReference nvarchar (255), 
    IsForceThrough bit, 
    NextTermPrePaid bit, 
    IsBillingMonthly bit, 
    LastPaymentDate datetime, 
    PaidToDate datetime, 
    IsIndeterminate bit 
    ) 

    INSERT INTO @MembersTable 
    SELECT * FROM [MembershipTermPerson] WHERE MembershipTermID = @MembershipTermID 


    --Vehicles 





    FETCH NEXT FROM cur INTO @MembershipTermID , @EndDate , @MembershipID <VARIABLES> 
END 

CLOSE cur 
DEALLOCATE cur 

回答

3

我覺得

喜歡的東西

INSERT INTO MyTable (ColA, ColB, ColC) 
SELECT 
    GETDATE(), A.MyCol, 'MyValue' 
FROM MyOtherTable A 
WHERE a.MyValue = 'What I Want' 

基本上你跳過臨時表,只是搶值和注入的一切,這將是一個INSERT INTO SELECT語句中一個很好的例子一旦。

+0

謝謝米切爾,我不知道這是可能的 – shicky 2012-01-10 19:38:36