求:在同一张表中根据name相同,把后面的text字段全部串成一个文件的SQL
有一张表:
no name text
1 a hello
2 a world
3 a haha
4 b ccc
5 b ddd
我想根据name字段,如果同名就显示:
a helloworldhaha
b ccdd
怎么用一条SQL语句(不用store procedure)来做到?
------解决方案-------------------- create table tb(no int,name varchar(3),text varchar(20))
insert into tb values(1, 'a','hello')
insert into tb values(2, 'a','world')
insert into tb values(3, 'a','haha')
insert into tb values(4, 'b','ccc')
insert into tb values(5, 'b','ddd')
go
create function dbo.f_str(@name varchar(3))
returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str+text from tb where name = @name
return @str
end
go
--调用函数
select name, text = dbo.f_str(name) from tb group by name
drop function dbo.f_str drop table tb
------解决方案--------------------为什么我的确不行啊 CREATE FUNCTION "EQQW"."F_FIELDSLICING_QW"
(N_DESC_NUM VARCHAR(100), N_DESC_TYPE VARCHAR(20))
RETURNS VARCHAR(20)
LANGUAGE SQL
SPECIFIC SQL121129090712500
BEGIN
declare @str varchar(1000);
set @str = '' ;
select @str = @str+CONTENT from EQMS.DTEWS7 where DESC_NUM = N_DESC_NUM AND DESC_TYPE = N_DESC_TYPE;
return @str ;
END 错误消息:DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=select;set @str = '' ;
;<psm_set_token>, DRIVER=3.57.82