--示例--示例数据
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 查找
--*/
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 查找
--*/
解决方案 »
- 这个sql,里面任何一个字段(字段有规律)等于搜索值,就输出。
- 求高效存储过程!!!
- 查询问题?
- 请问这样的问题能用一条SQL语句实现吗??
- SQL SERVER2000数据库备份的问题
- 2条sql语句
- SQL SERVER 2000+WINDOW 2000 SERVER 系统,但开发的软件只能在服务器本地跑,不能在客户机上运行,数据库连不上
- sql server2000服务器:Win2k3、CPU占用率近90%,有什么解决办法没?
- 关于数据库置疑后又附加的问题
- Microsoft® SQL Server™ 2000 中文版,支持几个并发链接?????
- Microsoft OLE DB Provider for ODBC Drivers 错误 '80004005'
- 请教一条sql语句写法!
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 模块')
----------- ----- ---- ----- ---- ----- ---- ----- ----
1 true 添加 true 删除 false 查看 false 更改
2 true 添加 false 删除 false 查看 true 更改
3 false 添加 false 删除 true 查看 false 更改
模块 添加 删除 修改 添加/删除
1 true false false false
2 true false true false
3 false flase false false
又怎么写了
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