27,579
社区成员
发帖
与我相关
我的任务
分享
if object_id('tb') is not null
begin
drop table tb
end
if object_id('f_g') is not null
begin
drop function f_g
end
go
create table tb(id varchar(64),GuiGe varchar(64))
insert into tb
select '001','300*300'
union all
select '001','300*400'
union all
select '002','200*300'
union all
select '003','100*100'
union all
select '001','400*400'
union all
select '002','400*400'
go
--code
create function f_g(@id varchar(64))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+'/'+GuiGe from tb
where id=@id
return stuff(@str,1,1,'')
end
go
select id,dbo.f_g(id) as 结果 from tb
group by id
-- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(Id int,GuiGe varchar(7))
Go
Insert into ta
select 001,'300*300' union all
select 001,'300*400' union all
select 002,'200*300' union all
select 003,'100*100' union all
select 001,'400*400' union all
select 002,'400*400'
Go
create table #a(Id int,GuiGe varchar(15),GuiGeStr varchar(100))
go
--Start
declare @IdOld varchar(15),@GuiGeStrs varchar(100)
select @IdOld='',@GuiGeStrs =''
insert into #a (Id,GuiGe ,GuiGeStr)
select Id,GuiGe ,'' from ta order by Id
update #a
set GuiGeStr =@GuiGeStrs,
@GuiGeStrs=case when Id=@IdOld then @GuiGeStrs+'/'+GuiGe else GuiGe end ,@IdOld=Id
select * from #a
drop table #a
--Result:
/*
Id GuiGe GuiGeStr
----------- --------------- ----------------------------------------------------------------------------------------------------
1 300*300 300*300
1 300*400 300*300/300*400
1 400*400 300*300/300*400/400*400
2 400*400 400*400
2 200*300 400*400/200*300
3 100*100 100*100
(所影响的行数为 6 行)
*/
--End
insert into #a (Id,GuiGe ,GuiGeStr)
select Id,GuiGe ,GuiGeStr,'' from Ab order by Id
这里插入字段都有问题.