您现在的位置是:网站首页> 编程资料编程资料
SQL server使用自定义函数以及游标_MsSql_
                     2023-05-26
                475人已围观
                
                2023-05-26
                475人已围观
            
简介 SQL server使用自定义函数以及游标_MsSql_
| 编号 | 标准宗地编码(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
您可能感兴趣的文章:
                
                
相关内容
- 很有意思的SQL多行数据拼接_MsSql_
- 设置密码保护的SqlServer数据库备份文件与恢复文件的方法_MsSql_
- SqlServer中的日期与时间函数_MsSql_
- SQL截取字符串函数分享_MsSql_
- SQL的小常识, 备忘之用, 慢慢补充._MsSql_
- SQL语句练习实例之七 剔除不需要的记录行_MsSql_
- SQL语句练习实例之六 人事系统中的缺勤(休假)统计_MsSql_
- SQL语句练习实例之五 WMS系统中的关于LIFO或FIFO的问题分析_MsSql_
- SQL语句练习实例之四 找出促销活动中销售额最高的职员_MsSql_
- sql存储过程获取汉字拼音头字母函数_MsSql_
 
                                
                                                         
                                
                                                         
                                
                                                         
 
    