这是一张人员的角色分配表:UserID RoleID RoleName
1 1 管理员
1 2 操作员
2 1 管理员
2 2 操作员
2 3 财务将人员拥有的角色用逗号分割显示: UserID RoleName
1 管理员,操作员
2 管理员,操作员,财务
请教高手这个SQL如何写!!!
1 1 管理员
1 2 操作员
2 1 管理员
2 2 操作员
2 3 财务将人员拥有的角色用逗号分割显示: UserID RoleName
1 管理员,操作员
2 管理员,操作员,财务
请教高手这个SQL如何写!!!
解决方案 »
- 存储过程语法问题
- 高手赐教 三表联合查询 急急急急急急急急急
- 请问在应用程序中直接将sql语句传到后台执行和应用程序调用存储过程执行,两者之间有什么具体的差别
- 想深入学习一下查询的优化、数据库优化、数据库管理方面知识,大家推荐几本好书吧。
- ms server 數據庫中的圖表有何作用?
- 在 Sql Server 2000 中返原 Sql Server 6.0 下的备份文件.dat
- 如何去掉开头的0
- 送分:)
- 恳请大力进来
- Sql Server中怎么利用临时表
- 我的操作系统是Windows Server2003.安装SQL-Server2000时,提示不支持.为什么?
- 我的操作系统是Windows Server2003.安装SQL-Server2000时,提示不支持.继续安装也不成功.为什么?
create table 表(UserID int,RoleID int,Rolename varchar(80))
insert into 表 select 1,1,'管理员'
insert into 表 select 1,2,'操作员'
insert into 表 select 1,1,'管理员'
insert into 表 select 2,2,'操作员'
insert into 表 select 2,3,'财务'go--创建用户定义函数
create function f_str(@Userid int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+RoleName from 表 where Userid = @Userid
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select Userid,人员=dbo.f_str(Userid) from 表 group by Userid order by Userid
go
returns varchar(200)
as
begin
declare @s_ret varchar(200)
set @s_ret = ''
select @s_ret = @s_ret + ',' + rolename from t_connect
where userid = @id
select @s_ret = stuff(@s_ret , 1 ,1 , '')
return @s_ret
end create table t_connect
(
userid int,
roleid int,
rolename varchar(20)
)insert into t_connect
select 1, 1, '管理员' union
select 1, 2, '操作员' union
select 2, 1, '管理员' union
select 2, 2, '操作员' union
select 2, 3, '财务'
select UserID ,dbo.f_connect(UserID) from t_connect
group by UserID