数据库-表别名帮助编译器正确解析

declare @abc table(
 name nvarchar(20),
 thedate nvarchar(20)
 )

 insert into @abc values('a','a2')
 insert into @abc values('b','b2')
 insert into @abc values('c','c2')
 insert into @abc values('b','b3')
 
select * from 
 (select ROW_NUMBER()over(partition by name order by thedate desc) as idd,name,thedate from @abc where thedate is not null )tt
 where tt.idd=1


像上面那样不会报错,可如果像下面这样就报错,可能是编译器不知道dd是哪个表的字段,无法编译所致,加了tt的话,编译器就知道idd是哪个表的字段了,可以编译。

select * from 
 (select ROW_NUMBER()over(partition by name order by thedate desc) as idd,name,thedate from @abc where thedate is not null )
 where idd=1

 

完。

 

posted @ 2014-12-19 11:10  liyou  阅读(214)  评论(0编辑  收藏  举报