日期:2014-05-17  浏览次数:20442 次

mssql 语句有问题,请帮忙看下
select  top  10 id   from [Product] where 1=1   and language=0 and lockid<>1   and  typeid in( 
with b as(
select id,name,parentid from ProductSort 
where id=12 
union all select x.id,x.name,x.parentid from ProductSort x,b 
where x.parentid=b.id 
)select id from b  
) order  by sortid asc , id  desc

---这种执行就有错
with b as(select id,name,parentid from ProductSort 
where id=12 
union all select x.id,x.name,x.parentid from ProductSort x,b 
where x.parentid=b.id 
)select id from b  
---上面单独执行就没错

select  top  10 id   from [Product] where 1=1   and language=0 and lockid<>1   and  
typeid in( 
12
) order  by sortid asc , id  desc
---上面单独执行也没错



为上面合到一起就会出错

------解决方案--------------------
;with b as(
select id,name,parentid from ProductSort 
where id=12 
union all select x.id,x.name,x.parentid from ProductSort x,b 
where x.parentid=b.id 
)
select  top  10 id   from [Product] where 1=1   and language=0 and lockid<>1   and  typeid in( 
select id from b  
) order  by sortid asc , id  desc

------解决方案--------------------
select  top  10 id   
from [Product] 
where 1=1   and language=0 and lockid<>1   and  typeid in
(select id from (
select id,name,parentid from ProductSort 
where id=12 
union all select x.id,x.name,x.parentid from ProductSort x,b 
where x.parentid=b.id 
)t   -->要有这个别名t

order  by sortid asc , id  desc


你的where 里面不能用with,还有两条语句连接查询后要有别名