SQL - How to track a field containing a sequence # from one stored proc and needed in another?

I have a stored proc that is using ROW_NUMBER to generate a specific sequence. Based on the last # generated, I need to use this # as a seed value in another stored proc to continue the sequence.

Is it possible to insert the last row number generated into a table? Is there a more elegant solution to this?

Thanks in advance!
nightshadzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Pawan KumarDatabase ExpertCommented:
>>Is it possible to insert the last row number generated into a table? Is there a more elegant solution to this?
Yes it is possible.

CREATE TABLE GetFirstProcData( col1 Datatype , col2 datatype.... )
EXEC Proc1 ...parameter1, parameter2...

DECLARE @rnk AS BIGINT
SELECT @rnk  = MAX(rownumbercolumn) FROM GetFirstProcData

Pass this @rnk in the stored procedure 2 as an input parameter..

In second stored procedure 2..you have to write like below--

SELECT allcolumns, @rnk + ROW_NUMBER() OVER (PARTITION BY columns.....ORDER BY columns..) newrnk....
From yourtable..

 

Open in new window

0
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
There are ways, but I am more worried about scaling up. How do you plan to handle things like multiple connections requesting the "next sequence" and one of them aborting or rolling back the transaction? Also, if each of these update the same column in a table, you will end up with large number of blocked transactions.

I would recommend evaluating the use of SEQUENCE, which is a new introduction in SQL Server 2012 (https://docs.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql).
2

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
Nakul's suggestion of using SEQUENCE is a good one as you don't need to create tables and manage much code.
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
SQL

From novice to tech pro — start learning today.