How to make SELECT NEXT VALUE work in SQL Server?

Alexander Kalian picture Alexander Kalian · Aug 30, 2013 · Viewed 53.9k times · Source

I'm having some difficulties trying to get the following to work in SQL Server:

CREATE TABLE table_X AS
    SELECT 
        NEXT VALUE FOR GROUP_A AS GROUP_ID, RISK_ID
    FROM 
        ( SELECT UNIQUE RISK_ID, FROM table_Y ) SS;

I am told:

"Incorrect syntax near the keyword 'SELECT'."
"Incorrect syntax near the keyword 'UNIQUE'."

What exactly is the problem and how can I fix this to work in SQL Server?

Answer

Sridhar picture Sridhar · Aug 30, 2013
CREATE TABLE Person
(
FirstName Varchar(50),
LastName Varchar(50)
);

/*
Insert Some Values Into Person
*/

CREATE SEQUENCE CountBy
    START WITH 1
    INCREMENT BY 1 ;

SELECT NEXT VALUE FOR CountBy OVER (ORDER BY LastName) AS ListNumber,
    FirstName, LastName
    INTO table_x
FROM Person