create function join_str(@content varchar(100))
returns varchar(2000)
as
begin
declare @str varchar(2000)
set @str=''
select @str=@str+','+rtrim(role2) from a_test where [name]=@content
select @str=right(@str,len(@str)-1)
return @str
end
go--调用:
select [name],dbo.join_str([name]) role2 from a_test group by [name]
returns varchar(2000)
as
begin
declare @str varchar(2000)
set @str=''
select @str=@str+','+rtrim(role2) from a_test where [name]=@content
select @str=right(@str,len(@str)-1)
return @str
end
go--调用:
select [name],dbo.join_str([name]) role2 from a_test group by [name]
解决方案 »
- ALTER TABLE T DROP column tname Cascade
- text 类型替换
- 求sql语句,来拿分
- 请问,怎样判断varchar型字段中的记录是不是数字?
- 请教怎么在在查询的结果前加一序列??
- 无法对表进行操作的问题:捆扰三年,发贴无数!!再次高分请教!
- OPENROWSET的问题,请帮忙!!!
- 邹建及各位老大,这样的叉交表怎么做,比较急!
- sp_executesql 问题
- 请问在asp中怎么用email的形式把当前页的信息发送给用户
- SQL数据库出现了连结错误,提示为一般性网络错误,请检查网络文档 connectionopen(preloginhandshake())
- 重复单号问题
insert csdn
select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'aa' union all
select 2,'bb' union all
select 2,'cc' union all
select 3,'aaa' union all
select 3,'bbb'
--select * from csdn
gocreate function Gettxt(@id int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s=''
select @s=@s +' ' +txt from csdn where id=@id
--return @s
return stuff(@s,1,1,'')
end
goselect id,dbo.Gettxt(id) txt from csdn group by id
godrop function Gettxt
drop table csdn
go
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
returns varchar(8000)
上面这个varchar(8000)是什么意思