如何使SELECT NEXT VALUE在SQL Server中工作?

时间:2013-08-30 11:03:48

标签: sql sql-server syntax syntax-error porting

我在尝试让以下内容在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;

我被告知:

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

问题究竟是什么?我如何解决这个问题?{1}}

3 个答案:

答案 0 :(得分:14)

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

答案 1 :(得分:2)

SELECT 
    NEXT VALUE FOR GROUP_A AS GROUP_ID, SS.RISK_ID
INTO
    table_X 
FROM 
    ( SELECT DISTINCT RISK_ID FROM table_Y ) SS

答案 2 :(得分:1)

试试这个 -

IF OBJECT_ID ('dbo.table_X') IS NOT NULL
   DROP TABLE dbo.table_X

SELECT RISK_ID --, ..... columns
INTO dbo.table_X
FROM ( 
    SELECT DISTINCT RISK_ID 
    FROM dbo.table_Y 
) SS

SELECT * 
FROM dbo.table_X