存储过程中,循环遍历临时表是一个很常见的操作,以下是个简单的例子:
declare @fl_field nvarchar ( 20 )
-- 定义游标
declare Temp_Table cursor for
select KdgId from kdg_type -- 查询结果
-- 打开游标 取第一行记录 赋给@fl_field
open Temp_Table
fetch next from Temp_Table into @fl_field
-- 循环开始
while @@fetch_status = 0
begin
-- 逻辑操作...
-- 取下一条记录
fetch next from Temp_Table into @fl_field
end
-- 循环结束
-- 关闭游标 删除游标引用
close Temp_Table
deallocate Temp_Table
用户自定义函数的递归实现,在树查询中较为常见:
/*
***** 查询@idValue下的所有子节点的id,用“,”隔开,拼接成字符串 *****
*/
/* ***** 返回结果的id排列符合树结构,即子节点在父节点后 ***** */
CREATE function dbo.Fun_GetChildren_KdgTypeData( @idValue varchar ( 20 ))
returns varchar ( 1000 )
as
begin
declare @itemId varchar ( 20 )
declare @temp varchar ( 1000 )
set @temp = @idValue
set @itemId = @idValue
declare Temp_Table cursor for
select KdgTypeId from kdg_typedata where IsDelete = 0 and KdgParentId = @itemId -- 查询@itemId的子节点
open Temp_Table
fetch next from Temp_Table into @itemId
-- 若@itemId不存在子节点
if @itemId is null or @itemId = ''
set @temp = @temp + @itemId -- 字符串拼接id + , + id
else
begin
while @@fetch_status = 0
begin
set @temp = @temp + ' , ' + dbo.Fun_GetChildren_KdgTypeData( @itemId ) -- 递归
fetch next from Temp_Table into @itemId
end
close Temp_Table
deallocate Temp_Table
end
return @temp
end
/* ***** 返回结果的id排列符合树结构,即子节点在父节点后 ***** */
CREATE function dbo.Fun_GetChildren_KdgTypeData( @idValue varchar ( 20 ))
returns varchar ( 1000 )
as
begin
declare @itemId varchar ( 20 )
declare @temp varchar ( 1000 )
set @temp = @idValue
set @itemId = @idValue
declare Temp_Table cursor for
select KdgTypeId from kdg_typedata where IsDelete = 0 and KdgParentId = @itemId -- 查询@itemId的子节点
open Temp_Table
fetch next from Temp_Table into @itemId
-- 若@itemId不存在子节点
if @itemId is null or @itemId = ''
set @temp = @temp + @itemId -- 字符串拼接id + , + id
else
begin
while @@fetch_status = 0
begin
set @temp = @temp + ' , ' + dbo.Fun_GetChildren_KdgTypeData( @itemId ) -- 递归
fetch next from Temp_Table into @itemId
end
close Temp_Table
deallocate Temp_Table
end
return @temp
end
[转载]数据库split函数的实现:
--
@SourceSql 需要分割的字符串
-- @StrSeprate 分隔符
create function f_split( @SourceSql varchar ( 8000 ), @StrSeprate varchar ( 10 ))
returns @temp table (a varchar ( 100 ))
-- 实现split功能 的函数
as
begin
declare @i int
set @SourceSql = rtrim ( ltrim ( @SourceSql ))
set @i = charindex ( @StrSeprate , @SourceSql )
while @i >= 1
begin
insert @temp values ( left ( @SourceSql , @i - 1 ))
set @SourceSql = substring ( @SourceSql , @i + 1 , len ( @SourceSql ) - @i )
set @i = charindex ( @StrSeprate , @SourceSql )
end
if @SourceSql <> ' '
insert @temp values ( @SourceSql )
return
end
-- @StrSeprate 分隔符
create function f_split( @SourceSql varchar ( 8000 ), @StrSeprate varchar ( 10 ))
returns @temp table (a varchar ( 100 ))
-- 实现split功能 的函数
as
begin
declare @i int
set @SourceSql = rtrim ( ltrim ( @SourceSql ))
set @i = charindex ( @StrSeprate , @SourceSql )
while @i >= 1
begin
insert @temp values ( left ( @SourceSql , @i - 1 ))
set @SourceSql = substring ( @SourceSql , @i + 1 , len ( @SourceSql ) - @i )
set @i = charindex ( @StrSeprate , @SourceSql )
end
if @SourceSql <> ' '
insert @temp values ( @SourceSql )
return
end
用法:select * from dbo.f_split('aaa,bbb,ccc,ddd,eee',',')
结果: