Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Identify records inserted in each batch

Author  Topic 

razeena
Yak Posting Veteran

54 Posts

Posted - 2011-09-15 : 09:15:46
Hi ,
I am taking 100 customerids from a customer table to a table variable and joining this with another
table say customerorder inside a while loop. The customerorder may contain more than one order for each customer.
While joining the custids in the temporary table
with the customerorder, suppose I got 20 records in the first batch of insertion and 50 records
in the second batch while
doing the 'insert into ... select'.
How can I retrieve the records inserted in each batch?

Kristen
Test

22859 Posts

Posted - 2011-09-15 : 09:22:19
INSERT INTO ... SELECT @BatchNo, OtherCol1, OtherCol2, ...
SELECT @BatchNo = @BatchNo + 1

??
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-09-15 : 10:51:14
Utilize the OUTPUT clause during your inserts.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2011-09-16 : 02:03:06
HI Razeena ... as Don suggested you can use output clause during your inserts like this..

INSERT INTO YourTable
output inserted.yourvalue1,
inserted.yourvalue2,
inserted.yourvalue3,... so on
INTO InsertedTable --If you want to insert the records is some kind of audit table than use this else commit it.
SELECT yourvalue1,yourvalue2,yourvalue3...so on


iF theRe iS a wAy iN tHen theRe iS a wAy oUt..
Go to Top of Page
   

- Advertisement -