Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7038937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:44:11+00:00 2026-05-28T01:44:11+00:00

So I am using a cursor to loop through a bunch of records that

  • 0

So I am using a cursor to loop through a bunch of records that my query returns. I have just updated some details in a table and now I want to pull the details from that table so I have used a temporary table.

So now I want to insert some values into a new table that are unrelated to the last and then the rest of the values would be a direct copy from the table variable…how can I do this?

I’ll post below the section in question to help people see what I am trying to do.
The part in question is between the update status comment and the above not finished comment.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:44:12+00:00Added an answer on May 28, 2026 at 1:44 am

    I think this would be a good case for a INSERT INTO SELECT statement

    Something like

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

    Basically you skip the temp table, and just grab the value and inject everything at once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using cursor to loop through a self join table. the query that i
I am trying to loop through a table using a cursor: DEClARE @ProjectOID as
I am using a while loop to iterate through a cursor and then outputing
I have some trouble when trying to update a table by looping cursor which
I have the following cursor (something like it anyways) that I am using to
Let's say I have following code cursor = connection.cursor() cursor.execute(query) after that point I
I have a a cursor loop that's building a string by concatenating the contents
I have a bad habit of using the cursor keys of my keyboard to
I've populated a ListActivity from a Cursor using SimpleCursorAdapter that starts another activity when
I'm looking for a way to loop through the columns of a table to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.