您的位置:首页 > 博客中心 > 数据库 >

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

时间:2022-03-14 04:02

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

 

完。

 

热门排行

今日推荐

热门手游