1. 程式人生 > >sql子查詢中引用無效物件名

sql子查詢中引用無效物件名

select AA.GoodsID from
(
 select A.GoodsID,A.SerNum from
 (select * from VStock where SerNum is not null) A
  left join InStorage_Record B on (A.GoodsID=B.GoodsID and A.SerNum=B.ISRSerNum)
) AA
where not exists
(select 1 from AA C where AA.GoodsID=C.GoodsID and AA.SerNum=C.SerNum and AA.ISNDate>C.ISNDate
 having count(1)>0)
因為AA物件名屬於from中的一個子查詢,在where中可以直接引用,但是在其下的子查詢中無法使用,這個時候可以採取with as結構解決。有關該結構的詳細使用可看下面文章:

http://blog.csdn.net/xueminxu/article/details/8132074