--示例--示例数据
create table [function](id int,功能id int,模块 varchar(10))
insert [function] select 1,1,'添加'
union  all        select 1,2,'删除'
union  all        select 2,1,'添加'
union  all        select 2,3,'更改'
union  all        select 2,5,'查找'
union  all        select 3,4,'查看'
go--查询
declare @s varchar(8000)
set @s=''
select @s=@s+',[功能'+cast(功能id as varchar)+']=max(case 功能id when '
+cast(功能id as varchar)+' then ''true'' else ''false'' end),功能名='''
+模块+''''
from [function] group by 功能id,模块exec('select 模块=id'+@s+' from [function] group by id')
go--删除测试
drop table [function]/*--测试结果模块          功能1   功能名  功能2   功能名  功能3   功能名  功能4   功能名  功能5   功能名  
----------- ----- ---- ----- ---- ----- ---- ----- ---- ----- ---- 
1           true  添加   true  删除   false 更改   false 查看   false 查找
2           true  添加   false 删除   true  更改   false 查看   true  查找
3           false 添加   false 删除   false 更改   true  查看   false 查找
--*/

解决方案 »

  1.   

    不好意思,我看错了,以为你是ACCESS版的。看了邹建老师的帖子发现T-SQL与SQL差很多啊
      

  2.   

    楼主试一下if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tb1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[tb1]
    GO 
    create table tb1
    (模块 int,
     功能号 int,
     功能名 varchar(50))insert into tb1 (模块,功能号,功能名) values('1','1','添加')
    insert into tb1 (模块,功能号,功能名) values('1','2','删除')
    insert into tb1 (模块,功能号,功能名) values('2','1','添加')
    insert into tb1 (模块,功能号,功能名) values('2','3','更改')
    insert into tb1 (模块,功能号,功能名) values('2','5','更改')
    insert into tb1 (模块,功能号,功能名) values('1','2','删除')
    insert into tb1 (模块,功能号,功能名) values('3','4','查看')select * from tb1declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',max(case  when 功能名='''+功能名+''' then ''true'' else ''false'' end) as 功能'+cast(max(功能号) as char(2))
    +','''+功能名+''''+'as 功能名'from tb1
    group by 功能名 order by max(功能号)exec('select 模块'+@sql+' from tb1 group by 模块')
      

  3.   

    测试结果如下:模块          功能1   功能名  功能2   功能名  功能4   功能名  功能5   功能名  
    ----------- ----- ---- ----- ---- ----- ---- ----- ---- 
    1           true      添加   true     删除    false    查看   false   更改
    2           true      添加   false    删除    false    查看   true    更改
    3           false     添加   false    删除    true     查看   false   更改
      

  4.   

    我想把他们变成
    模块    添加           删除       修改       添加/删除
    1         true         false      false      false  
    2         true         false      true        false             
    3         false        flase      false       false      
    又怎么写了        
      

  5.   

    已解决了
    USE GPECSTORE 
    GOcreate table t(functionid char(10),propertyid char(20),propertyname char(20))
    go insert into tselect AppFu.FunctionID,PropertyID,propertyname from ApplicationFunction as AppFu, RightProperty as RiP, RoleRight as RoR where RoR.FunctionID=AppFu.FunctionID and RoR.RightID=RiP.RightID
    godeclare @s varchar(8000)
    set @s=''select @s=@s+',['+propertyname+']=max(case propertyid when '
    +cast(propertyid as varchar)+' then ''true'' else ''false'' end)'
    from [t] group by propertyname,propertyidexec('select functionid=functionid'+@s+' from [t] group by functionid')
    go
    drop table t