编号 |
标准宗地编码(landCode) |
所在区段编码(sectCode) |
1 |
131001BG001 |
G001 |
2 |
131001BG002 |
G001 |
3 |
131001BG003 |
G001 |
4 |
131001BG004 |
G002 |
5 |
131001BG005 |
G003 |
现在需要将表中的数据转换为如下表所示结果:
编号 |
区段编码 |
包含的标准宗地 |
1 |
G001 |
131001BG001,131001BG002,131001BG003 |
2 |
G002 |
131001BG004 |
3 |
G003 |
131001BG005 |
代码如下:
create function combstr(@name nvarchar(50))
returns nvarchar(300)
as
begin
declare @resultStr nvarchar(300)
declare @tempStr nvarchar(500)
declare @flag int
declare myCur cursor --定义游标
For(select landCode from land where sectCode=@name )
open myCur –-打开游标
fetch next from myCur into tempStr –将游标下移
set @flag=0
while @@fetch_status=0
begin
if @flag=0
begin
set @resultStr=@tempStr
end
else
begin
set @resultStr=@resultStr+','+@tempStr
end
set @flag=@flag+1
fetch next from myCur into @tempStr
end
close myCur
deallocate myCur
return @result
end
returns nvarchar(300)
as
begin
declare @resultStr nvarchar(300)
declare @tempStr nvarchar(500)
declare @flag int
declare myCur cursor --定义游标
For(select landCode from land where sectCode=@name )
open myCur –-打开游标
fetch next from myCur into tempStr –将游标下移
set @flag=0
while @@fetch_status=0
begin
if @flag=0
begin
set @resultStr=@tempStr
end
else
begin
set @resultStr=@resultStr+','+@tempStr
end
set @flag=@flag+1
fetch next from myCur into @tempStr
end
close myCur
deallocate myCur
return @result
end