读书人

mssql 语句有有关问题请帮忙看下

发布时间: 2013-10-15 16:47:37 作者: rapoo

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,还有两条语句连接查询后要有别名

读书人网 >SQL Server

热点推荐