a 程序员
b 前台接待
c 销售
d 客服
a 前台接待
a 销售
a 客服
b 客服我想把这个表数据取出来建立一个sql视图a 程序员,前台接待,销售,客服
b 前台接待,客服
c 销售
d 客服
也就是a公司的名在视图的一个字段 ,他所有的公司职位合在一起一个字段非常感谢
b 前台接待
c 销售
d 客服
a 前台接待
a 销售
a 客服
b 客服我想把这个表数据取出来建立一个sql视图a 程序员,前台接待,销售,客服
b 前台接待,客服
c 销售
d 客服
也就是a公司的名在视图的一个字段 ,他所有的公司职位合在一起一个字段非常感谢
---------------------------------------------------------------------
--生成测试数据
create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
Create Function F_GetName(@ID Varchar(10))
Returns Nvarchar(1000)
As
Begin
Declare @S Nvarchar(1000)
Select @S = ''
Select @S = @S + Name + ',' From TEST Where ID = @ID
Select @S = Left(@S, Len(@S) - 1)
Return @S
End
GO
--建立視圖
Create View V_TEST
As
Select
ID,
dbo.F_GetName(ID) As Name
From
TEST
Group By
ID
GO
--測試
Select * From V_TEST
Create Table TEST
(ID Varchar(10),
Name Nvarchar(100))
--插入數據
Insert TEST Select 'a', N'程序员'
Union All Select 'b', N'前台接待'
Union All Select 'c', N'销售'
Union All Select 'd', N'客服'
Union All Select 'a', N'前台接待'
Union All Select 'a', N'销售'
Union All Select 'a', N'客服'
Union All Select 'b', N'客服'
GO
--建立函數
Create Function F_GetName(@ID Varchar(10))
Returns Nvarchar(1000)
As
Begin
Declare @S Nvarchar(1000)
Select @S = ''
Select @S = @S + Name + ',' From TEST Where ID = @ID
Select @S = Left(@S, Len(@S) - 1)
Return @S
End
GO
--建立視圖
Create View V_TEST
As
Select
ID,
dbo.F_GetName(ID) As Name
From
TEST
Group By
ID
GO
--測試
Select * From V_TEST
GO
--刪除測試環境
Drop Table TEST
Drop Function F_GetName
Drop View V_TEST
--結果
/*
ID Name
a 程序员,前台接待,销售,客服
b 前台接待,客服
c 销售
d 客服
*/
帮你Ding
drop table tb
gocreate table tb(id varchar(10),content varchar(10))
insert into tb(id,content) values('a', '程序员')
insert into tb(id,content) values('b', '前台接待')
insert into tb(id,content) values('c', '销售')
insert into tb(id,content) values('d', '客服')
insert into tb(id,content) values('a', '前台接待')
insert into tb(id,content) values('a', '销售')
insert into tb(id,content) values('a', '客服')
insert into tb(id,content) values('b', '客服')
go
if object_id('pubs..f_hb') is not null
drop function f_hb
go--创建一个合并的函数
create function f_hb(@id varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(content as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as content from tbdrop table tb/*
id content
---------- -------------------------
a 程序员,前台接待,销售,客服
b 前台接待,客服
c 销售
d 客服(所影响的行数为 4 行)*/