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 7947419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:25:36+00:00 2026-06-04T01:25:36+00:00

requirement is to transfer all inserted records to one table to another. i’ve used

  • 0

requirement is to transfer all inserted records to one table to another. i’ve used a trigger to do it. i’m looping through all the inserted records and inserting to new table one record at a time as i have to increment a sequence number in the destination table. but this loop considarably slow when number of inserted rows increase. is there a better way of doing this.

Declare @maxpk int, @count int, @seq int
set @maxpk=(select max(refno) from inserted )
set @count=(select count(1) from inserted)
set @seq=((select max(seq_no) from dbase.dbo.destination))

while @count>0
begin
set @seq=(select @seq+1)
insert into dbase.dbo.destination(orderno,SEQ_NO,PRODUCT_ID,qty)
select  ordernumber,@seq,productid ,quantity
from inserted where refno=@maxpk
set @count=(select @count-1)
set @maxpk=(select top 1 refno from inserted where refno<@maxpk)
end

refno is primary key of source table. is there a way to check the end of inserted records so i don’t have to initialize and maintain a loop counter?

and can loop be executed for each record in inserted table so i don’t have to find the next record to insert by comparing the value of primary key.
using mssql 2005

  • 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-06-04T01:25:37+00:00Added an answer on June 4, 2026 at 1:25 am

    This should handle concurrency ok but I really think you need to revisit the design (e.g. make seq_no an IDENTITY column, then the system generates the unique values for you, and handles concurrency too).

    CREATE TRIGGER dbo.SourceTrg ON dbo.Source
    AFTER INSERT
    AS
    BEGIN
        SET NOCOUNT ON;
    
        DECLARE @seq INT;
        SET @seq = COALESCE((SELECT MAX(seq_no) 
          FROM dbase.dbo.Destination WITH (TABLOCKX, HOLDLOCK)), 0);
    
        INSERT dbase.dbo.Destination(SEQ_NO, orderno, PRODUCT_ID, qty)
          SELECT @seq + ROW_NUMBER() OVER (ORDER BY refno),
            ordernumber, productid, quantity
          FROM inserted;
    END
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Requirement: I need to run multiple animations one by one. I'm using onAnimationEnd() to
My requirement is to create an Enum based on values present in a table
I need to transfer all the files dropped on an element to a server
I am working through the requirement to make a WPF Application single instance only.
What are possible options to transfer a lot of data from one computer to
Requirement is this ... We have our 3 web-applications deployed in RHEL-5 server, we
Requirement : Should update MySQL database with that of MS Sql Server updates which
Requirement is like: I want to take input values from user in frame but
Requirement of my app is to have UINavigationController at the bottom with play, rewind
the Requirement in simple words goes like this. Its a charting Application ( kinda

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.