首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-09-25 
sqlserver实现取相同名称放在同一字段if object_id([tbtest]) is not null drop table [tbtest] create

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

热点排行