当前位置: 代码迷 >> Sql Server >> 有个SELECT语句运作通不过,请高人指点。
  详细解决方案

有个SELECT语句运作通不过,请高人指点。

热度:80   发布时间:2016-04-24 09:23:14.0
有个SELECT语句运行通不过,请高人指点。。。
select top 2 * from (SELECT identity(int,1,1) AS 
RowNumber,P.*,C.CarNum into #TableA FROM Position P inner join CarInfo C on C.CardID=P.CardID
 ORDER BY P.PositioningTime asc) where RowNumber > 1000*(1-1) and  CardId='145066' and PositioningTime between '2014-12-08 00:00:00' and '2015-01-06 14:32:10'


服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'identity' 附近有语法错误。

如果把语句拆分成两句运行就正常。。
SELECT identity(int,1,1) AS 
RowNumber,P.*,C.CarNum into #TableA FROM Position P inner join CarInfo C on C.CardID=P.CardID
 ORDER BY P.PositioningTime asc


select top 2 * from #TableA where RowNumber > 1000*(1-1) and  CardId='145066' and PositioningTime between '2014-12-08 00:00:00' and '2015-01-06 14:32:10'

------解决思路----------------------
语法问题,SELECT ... INTO 句式不返回结果

你的查询方式,必须拆开来写
------解决思路----------------------
--SQL Server 2000
select top 2 identity(int,1,1) as RowNumber,P.*,C.CarNum
into #TableA
from Position P
inner join CarInfo C on C.CardID=P.CardID
where RowNumber > 1000*(1-1) and  CardId='145066' 
and PositioningTime between '2014-12-08 00:00:00' and '2015-01-06 14:32:10'
order by P.PositioningTime asc;

--注意,identity不能保证按照排序的顺序插入TableA,请参考Msdn的说明。
--另一个解决方案是,先建立临时表#TableA,以identity为主键,自动分配ID。
  相关解决方案