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

  • Home
  • SEARCH
  • 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 4546526
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:57:58+00:00 2026-05-21T15:57:58+00:00

if I want to make Multiple operation on each row of a result set,

  • 0

if I want to make Multiple operation on each row of a result set, do I must use loop or cursor. Is there any other effective or convenience way to do that? I am writing a long stored procedure, I have used a readonly cursor for the outside loop.I also need some loop inside loop.
(I am new programmer, and my english is not good,I hope some one can help me.)

  • 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-21T15:57:59+00:00Added an answer on May 21, 2026 at 3:57 pm

    There’s still not enough information in your question to give a good answer. I’ll share some code that may be “adequate”, to help you see one way of re-casting a question to avoid cursors/loops. Say we have a table which we’re using as a queue (called Queue). For this simple example, all we’re storing is a value (Action) that we want inserted later into another table (called Target):

    create table Queue (
        QueueID int IDENTITY(1,1) not null,
        Action varchar(10) not null,
        Processed bit not null,
        TargetID int null,
        constraint PK_Queue PRIMARY KEY (QueueID),
        constraint CK_Queue_Processed CHECK ( Processed = 0 or TargetID is not null)
    )
    go
    create table Target (
        TargetID int IDENTITY(1,1) not null,
        Action varchar(10) not null,
        constraint PK_Target PRIMARY KEY (TargetID)
    )
    

    Note that, once an item has been processed from the queue table, we want to mark it as having been processed (Processed=1) and we want to know which row we eventually inserted into the target table (TargetID).

    So, let’s create some sample rows to process:

    insert into Queue(Action,Processed)
    select 'abc',0 union all
    select 'def',0 union all
    select 'ghi',0
    

    And now, we’ll process this queue, first inserting rows into the target table, and then updating the queue table appropriately:

    declare @Inter table (QueueID int not null,TargetID int not null)
    
    ;merge into Target using (select QueueID,Action from Queue where Processed=0) Queue
    on 1=0
    when not matched then insert (Action) values (Action)
    output Queue.QueueID,inserted.TargetID into @Inter (QueueID,TargetID);
    
    update q
    set
        Processed = 1,
        TargetID = i.TargetID
    from
        Queue q
            inner join
        @Inter i
            on
                q.QueueID = i.QueueID
    

    We could have used a simpler insert statement (rather than the merge), if we were inserting enough information into the target table (e.g. QueueID) such that we could retrieve everything we need in the @Inter table from the inserted pseudo table.

    And finally, we check both tables to ensure that it’s worked as we expected:

    select * from Queue
    select * from Target
    

    On my machine, the TargetIDs assigned to the rows matched the QueueIDs, but that is not guaranteed.

    The above is a re-working of my answer to How Can I avoid using Cursor for implementing this pseudo code – SQL Server, which included performing a further insert into a second table before updating the original table.

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

Sidebar

Related Questions

I want to make website in multiple languages, I do it right with cookies
I want to make a view where I can select multiple items from listview
in order to make things easier for users i want to add multiple keyword
I have read multiple articles on here concerning arrays, .each, and other solutions, but
I want to make a Configuration Data Manager. This would allow multiple services to
I want to make an application with multiples of nine. You must think at
Is there anyway to make a quicksort sort by multiple conditions? For example, I
Well I want to make my Multiple (actually only 2) order by to work
i want to make multiple Markers google map by address or pincode like i
I want make datetimepicker in my project. Using jquery how it is possible? I

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.