declare @str varchar(8000) set @str='' select @str=@str+des from tb1 where des is not null select @str 即可
--自定义函数 create function strSum() returns varchar(8000) as begin declare @r varchar(8000) set @r='' select @r=@r+des from tb1 return(@r) end go--调用 select dbo.strsum() from tb1
--应该是这样调用 select dbo.strsum()
--测试--测试数据 create table tb1(id int,des varchar(10)) insert tb1 select 1,'abc' union all select 2,'def' union all select 3,'ghi' go--自定义函数 create function strSum() returns varchar(8000) as begin declare @r varchar(8000) set @r='' select @r=@r+des from tb1 return(@r) end go--调用 select dbo.strsum() go--删除测试 drop table tb1 drop function strsum/*--测试结果------------------------------ abcdefghi(所影响的行数为 1 行) --*/
能否把它当成系统提供的函数(如Sum)一样用?也就是说,如果这样调用select strSum(des) from tb1 where id <= 2应该得到'abcdef'
set @str=''
select @str=@str+des from tb1 where des is not null
select @str
即可
--自定义函数
create function strSum()
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+des from tb1
return(@r)
end
go--调用
select dbo.strsum() from tb1
select dbo.strsum()
create table tb1(id int,des varchar(10))
insert tb1 select 1,'abc'
union all select 2,'def'
union all select 3,'ghi'
go--自定义函数
create function strSum()
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+des from tb1
return(@r)
end
go--调用
select dbo.strsum()
go--删除测试
drop table tb1
drop function strsum/*--测试结果------------------------------
abcdefghi(所影响的行数为 1 行)
--*/
能否把它当成系统提供的函数(如Sum)一样用?也就是说,如果这样调用select strSum(des) from tb1 where id <= 2应该得到'abcdef'