求一动态SQL查询语句,高手们帮看看!
正在做后台添加模块.
MG_ID MG_NameMG_DescriptMG_ParentID
10000系统设置系统设置0
10001视学网后台管理视学网后台管理0
20001教育书店管理教育书店管理10001
20002讲师讲堂管理讲师讲堂管理10001
20003九大频道管理九大频道管理10001
20004播客管理播客管理10001
20005论坛博客管理论坛博客管理10001
20006博客管理博客管理10001
//根节点
select max(MG_ID) from CE_MG_MenuPopedom where MG_ParentID=0
//二级节点
select max(MG_ID) from CE_MG_MenuPopedom where MG_ParentID=10001
...这样是写死的
如何动态查找出每个节点下的最大的MG_ID
[解决办法]
--??这样??
declare @MG_ID varchar(20),@MG_ParentID varchar(20)
set @MG_ParentID=??
exec sp_executesql
N 'select @MG_ID=max(MG_ID) from CE_MG_MenuPopedom where MG_ParentID=MG_ParentID ',
N '@MG_ID varchar(20) output, MG_ID@MG_ParentID varchar(20) ',
@MG_ID output,@MG_ParentID
select @MG_ID
[解决办法]
--??这样??
select MG_ID=max(MG_ID) from CE_MG_MenuPopedom group by MG_ParentID
[解决办法]
if exists(select 1 from sysobjects where name = '#temp ' and type= 'U ')
drop table #temp
create table #temp(MG_ParentID VARCHAR(50))
select MG_ParentID
insert #temp
from CE_MG_MenuPopedom
group by MG_ParentID
select max(MG_ID),MG_ParentID
from CE_MG_MenuPopedom A INNER JOIN #temp B
ON A.MG_ParentID=B.MG_ParentID
group by MG_ParentID
DROP TABLE #TEMP