`
luck332
  • 浏览: 84977 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

sqlserver实现取相同名称放在同一字段

阅读更多

if object_id('[tbtest]') is not null drop table [tbtest]
create table tbtest
(
A varchar(10) not null,
B varchar(50) not null
)
go
insert into tbtest
select 'A',1 union all
select'A',2 union all
select'A',3 union all
select'B',4 union all
select'B',5 union all
select'B',6 union all
select'B',7 union all
select'C',8 union all
select'C',9 union all
select'C',10 union all
select'C',11

go

if object_id('[dbo].[functiontest]') is not null drop function [dbo].[functiontest]
go

create function functiontest()
returns @tb table(A varchar(10),B varchar(50))
AS
begin
declare @a varchar(50)
declare @b varchar(50)
declare temp_cur cursor local for
select * from tbtest
open temp_cur
fetch next from temp_cur into @a,@b
while (@@fetch_status = 0)
begin
if(not exists(select * from @tb where A = @a))
insert into @tb(A,B)values(@a,@b)
else
update @tb set B = B + ',' + @b where A = @a
fetch next from temp_cur into @a,@b
end
return
end
go

select * from functiontest()
go

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics