SQL表变量,用于插入具有其他值的其他表

时间:2012-01-10 19:22:35

标签: sql sql-server

所以我使用游标循环遍历查询返回的一堆记录。我刚刚更新了表中的一些细节,现在我想从该表中提取详细信息,因此我使用了临时表。

所以现在我想将一些值插入到与最后一个无关的新表中,然后其余的值将是表变量的直接副本...我该怎么做?

我会在相关部分下方发帖,以帮助人们了解我要做的事情。 有问题的部分介于更新状态评论和上述未完成评论之间。

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

1 个答案:

答案 0 :(得分:3)

我认为这对于INSERT INTO SELECT语句来说是个好例子

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

基本上你跳过临时表,只需抓住值并立即注入所有内容。