Sql code to create the sequence based on the Maximum value of the column of the tables in azure sql database

Rahul 251 Reputation points
2023-12-14T06:01:57.66+00:00

Hi,

Question-1- Please help with the sql code to create the sequence based on the maximum value of the column of the table in the Azure sql database ?

Column Name- A

Table Name- ABC

Azure SQL Database
{count} votes

Accepted answer
  1. SSingh-MSFT 16,371 Reputation points Moderator
    2023-12-18T07:16:44.6433333+00:00

    Hi Kashish •,

    Please refer to the below GIF, this should help:

    GIFSequenceDesc.gif

    Here sample code is used as below:

    CREATE SEQUENCE sequence_1
    start with 100
    increment by -1
    minvalue 0
    maxvalue 100
    cycle;
    

    and

    SELECT NEXT VALUE FOR sequence_1 AS Counter;

    Let us know if you have further queries. Thanks

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. SSingh-MSFT 16,371 Reputation points Moderator
    2023-12-14T07:18:03.6766667+00:00

    Hi Kashish •,

    Welcome to Microsoft Q&A forum.

    As I understand, you want to create sequence in Azure SQL Database based on the maximum value of the column of the tables.

    Please refer to the documentation here https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver16 where syntax and other details are given for creation of sequence in Azure SQL Database.

    You can use [ CYCLE | NO CYCLE ]
    Property that specifies whether the sequence object should restart from the minimum value (or maximum for descending sequence objects) or throw an exception when its minimum or maximum value is exceeded. The default cycle option for new sequence objects is NO CYCLE.

    Note:

    Cycling a SEQUENCE restarts from the minimum or maximum value, not from the start value.

    Hope this helps.

    Let us know if you have further queries.

    Thanks

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.