Identify and select records that were just inserted

I'm trying to find out the best way to identify the records that were just inserted and select data fields from those records using a stored procedure.
The task is to retrieve certain fields from the entries that were inserted in bulk from a flat file.
To base it on creation date could be an issue as this could be an overnight process and dates will not match. Should I add a flag field that gets set for an insert and reset when its processed.
Or is there a way using cursors or identity I could achieve this.  Please let me know your ideas. Thank You
darthvader747Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:

DECLARE @tab TABLE (id int)
INSERT INTO tableName
OUTPUT INSERTED.id INTO @tab(id ) --now @tab conatin the newly inserted ids
SELECT col1 ,col2
FROM someTable
0
darthvader747Author Commented:
The records are inserted in a different method. Could you please explain your solution in this context. Thank You
0
cyberkiwiCommented:
>> Should I add a flag field that gets set for an insert and reset when its processed.
That is by far the simplest efficient approach.  Flags (bit) are not optimised and cannot be indexed nicely.
Instead, add a datetime field to the table default getdate() that is not supplied by the flat file import.
Add an index to it too.
Clear the datetime field once processed, which also keeps the index small (nulls are never indexed).

You could use an identity column, but that means you need to keep track of the last identity processed from the last batch each time.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
I would recommend you use the OUTPUT clause as suggested by Aneesh.  The big advantage is that you do not have make any schema changes.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.