create function f_getother(@uid int)
returns nvarchar(1000)
as
declare @s nvarchar(1000)
set @s=''
select @s=@s+' '+other from tablename where uid=@uid
return(stuff(@s,1,1,''))
goselect uid,dbo.f_getother(uid) as other from tablename group by uid
returns nvarchar(1000)
as
declare @s nvarchar(1000)
set @s=''
select @s=@s+' '+other from tablename where uid=@uid
return(stuff(@s,1,1,''))
goselect uid,dbo.f_getother(uid) as other from tablename group by uid
create function f_getother(@uid int)
returns nvarchar(1000)
as
declare @s nvarchar(1000)
set @s=''
select @s=@s+' '+other from tablename where uid=@uid
return(stuff(@s,1,1,''))
go
----调用函数
select uid,dbo.f_getother(uid) as other from tablename group by uid
Create Table 表(UID varchar(10),OTHER varchar(10),id integer)
--插入数据
insert into 表
select '1','HELLO',1 union
select '1','WORLD!',2 union
select '2','I',3 union
select '2','Love',4 union
select '2','Delphi',5
select * from 表
--测试语句
go
CREATE FUNCTION FunMergeCharField(@vchA varchar(10))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r=''
SELECT @r=@r+' '+OTHER FROM 表 WHERE UID=@vchA order by id
RETURN(substring(@r,2,8000))
END
GOselect uid,dbo.FunMergeCharField(uid) as OTHER from 表 group by uid
--删除测试环境
Drop Table 表
Drop Function FunMergeCharField
1 HELLO WORLD!
2 I Love Delphi*/
--创建函数
Create function GetOther(@UID int)
Returns nvarchar(1000)
As
declare @s nvarchar(1000)
set @s=''
select @s=@s+' '+OTHER from tablename where UID=@UID
return(stuff(@s,1,1,''))
Go
--测试
select UID,dbo.GetOther(UID) As OTHER from 表名 group by UID
Create Table 表(UID varchar(10),OTHER varchar(10),id integer)
--插入数据
insert into 表
select '1','HELLO',1 union
select '1','WORLD!',2 union
select '2','I',3 union
select '2','Love',4 union
select '2','Delphi',5
select * from 表
--测试语句
go
CREATE FUNCTION FunMergeCharField(@vchA varchar(10))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r=''
SELECT @r=@r+' '+OTHER FROM 表 WHERE UID=@vchA order by id
RETURN(substring(@r,2,8000))
END
GOselect uid,dbo.FunMergeCharField(uid) as OTHER from 表 group by uid
--删除测试环境
Drop Table 表
Drop Function FunMergeCharField