sql server中的嵌套选择查询

时间:2014-06-03 13:06:01

标签: mysql sql sql-server oracle tsql

我正在努力将我的数据库从Oracle迁移到Sql Server。 我有一个表定义如下(在MS SQL Server 2012中):

CREATE TABLE DEMO_TABLE(
ID BIGINT IDENTITY(1,1) NOT NULL,
COLUMN_1 BIGINT NULL,
COLUMN_2 VARCHAR(256) DEFAULT NULL,
DATE_CREATED DATETIMEOFFSET DEFAULT SYSDATETIMEOFFSET() NULL,
OPERATIONS NUMERIC(10,0) DEFAULT 0 NULL,
CONSTRAINT PK_DEMO_TABLE PRIMARY KEY (ID)
)

工作正常。 现在,我在Oracle中有一个select语句如下:

select * from (select 
            COLUMN_1,
            COLUMN_2,
            DATE_CREATED,
            OPERATIONS,
            SYSTIMESTAMP,
            row_number()
        over (order by date_created DESC) rn 
        from 
            DEMO_TABLE 
        where 
            COLUMN_1 = 3 AND 
            OPERATIONS IN 
            (1,2,3,4)
            AND COLUMN_2='sometext'
            AND DATE_CREATED <= SYSTIMESTAMP        
        ) where rn between 1 and 100 order by rn

我在将此select语句从Oracle转换为SQL服务器时遇到问题。 此查询在Oracle中正常工作,但在SQL Server中出现以下错误:

Msg 156, Level 15, State 1, Line 17 Incorrect syntax near the keyword 'where'.

我无法弄清楚这里有什么问题。请帮忙!

1 个答案:

答案 0 :(得分:1)

select * from 
(select 
            COLUMN_1,
            COLUMN_2,
            DATE_CREATED,
            OPERATIONS,
            SYSTIMESTAMP,
            row_number()
        over (order by date_created DESC) rn 
        from 
            DEMO_TABLE 
        where 
            COLUMN_1 = 3 AND 
            OPERATIONS IN 
            (1,2,3,4)
            AND COLUMN_2='sometext'
            AND DATE_CREATED <= SYSTIMESTAMP        
        ) r
        where r.rn between 1 and 100 order by r.rn