Can a stored proc , return different values each time , incremented by 1

  • Can we write a stored procedure , which will return different value each time , incremented by 1

    Like first time we call the stored procedure , it should return 1

    seond time , it should return , 2

    thrd time , it should return 3

    4th time , it should return 1

    5th time , it should return 2

    6th time , it should return 3 .

    So after every 3rd call, it should return 1

  • Kinda, create a table to store the value and increment/decrement the value as appropriate when the procedure is called. You'll have to watch locking/isolation levels to ensure the correct results when two or more connections call the procedure simultaneously.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • You can also use SEQUENCE feature of SQL Server to get this functionality. However SEQUENCE are only available from SQL Server 2012 onwards.


    Sujeet Singh

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply