The below-mentioned are the Oracle SQL queries to generate the Sequence ID. Can anyone please help in rewriting the same queries compatible with MS SQL, to produce the same result?

  1. DB Sequence: -

    create sequence my_sequence start with 1
    
  2. Create a function get_next_code that will use the sequence and return the incremented value (as string): -

    create or replace function get_next_code return varchar2 as code number; 
    begin select my_sequence.nextval into code from dual; 
    return to_char(code); 
    end;
    
  3. Create a view which will pass the incremented value: -

    create or replace view my_counter (my_code) as (select get_next_code() from dual);
    
    

Query used to get the sequence ID generated(This is the query used in the java program):

select my_code from my_counter

Expecting the same result in MS SQL
Need the steps to create an MSSQL View/Table with only one column and one row that generates a Sequence ID, when I run a select query every time.

1

There are 1 best solutions below

0
Charlieface On

In SQL Server you create sequences in a similar way.

CREATE SEQUENCE my_sequence START WITH 1;

Then to get the next value

SELECT NEXT VALUE FOR my_sequence;

You can also use sp_sequence_get_range to get a range of numbers.