sql获取到table一般如何处理循环

qingYun1029 2013-11-29 06:24:49
系统中已经定义好了函数f_split

调用该函数获取到的是一张表,通过提供字符截取后的表。

例如:

格式为:projectId:proCount,

然后我需要循环获取到的这个表来操作;

请问该怎么循环这个表格??
...全文
307 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 10 楼 qingYun1029 的回复:
[quote=引用 8 楼 yupeigu 的回复:] [quote=引用 7 楼 qingYun1029 的回复:] [quote=引用 6 楼 yupeigu 的回复:] 你的函数是类似下面的函数把,这样返回了多行值,你要怎么循环处理呢,能说一下不

--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 



select *
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2
/*
任务id	奖励	col
2	6:16,7:23,8:28	6:16
2	6:16,7:23,8:28	7:23
2	6:16,7:23,8:28	8:28
*/
每一行都是两个数字,这两个数字是传递给另外一个存储过程的两个参数。。 所以每一行都需要调用存储过程。。。 谢谢![/quote] 这样呢:



--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 


declare @t table(id int identity(1,1),a int, b int)
declare @i int
declare @count int

insert into @t
select left(t.col,charindex(':',col)-1),
       SUBSTRING(t.col,charindex(':',col)+1,LEN(t.col))
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2

set @i =1
set @count = (select COUNT(*) from @t)

--循环
while @i <= @count
begin
	select a,b from @t where id = @i
	
	set @i = @i + 1
end

[/quote] 所以里面的from tb不需要,后面的任务id=2也不需要,怎么改呢?[/quote] 我加你关注了,可以发私信。
qingYun1029 2013-12-02
  • 打赏
  • 举报
回复
引用 8 楼 yupeigu 的回复:
[quote=引用 7 楼 qingYun1029 的回复:] [quote=引用 6 楼 yupeigu 的回复:] 你的函数是类似下面的函数把,这样返回了多行值,你要怎么循环处理呢,能说一下不

--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 



select *
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2
/*
任务id	奖励	col
2	6:16,7:23,8:28	6:16
2	6:16,7:23,8:28	7:23
2	6:16,7:23,8:28	8:28
*/
每一行都是两个数字,这两个数字是传递给另外一个存储过程的两个参数。。 所以每一行都需要调用存储过程。。。 谢谢![/quote] 这样呢:



--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 


declare @t table(id int identity(1,1),a int, b int)
declare @i int
declare @count int

insert into @t
select left(t.col,charindex(':',col)-1),
       SUBSTRING(t.col,charindex(':',col)+1,LEN(t.col))
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2

set @i =1
set @count = (select COUNT(*) from @t)

--循环
while @i <= @count
begin
	select a,b from @t where id = @i
	
	set @i = @i + 1
end

[/quote] 所以里面的from tb不需要,后面的任务id=2也不需要,怎么改呢?
qingYun1029 2013-12-02
  • 打赏
  • 举报
回复
引用 8 楼 yupeigu 的回复:
[quote=引用 7 楼 qingYun1029 的回复:] [quote=引用 6 楼 yupeigu 的回复:] 你的函数是类似下面的函数把,这样返回了多行值,你要怎么循环处理呢,能说一下不

--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 



select *
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2
/*
任务id	奖励	col
2	6:16,7:23,8:28	6:16
2	6:16,7:23,8:28	7:23
2	6:16,7:23,8:28	8:28
*/
每一行都是两个数字,这两个数字是传递给另外一个存储过程的两个参数。。 所以每一行都需要调用存储过程。。。 谢谢![/quote] 这样呢:



--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 


declare @t table(id int identity(1,1),a int, b int)
declare @i int
declare @count int

insert into @t
select left(t.col,charindex(':',col)-1),
       SUBSTRING(t.col,charindex(':',col)+1,LEN(t.col))
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2

set @i =1
set @count = (select COUNT(*) from @t)

--循环
while @i <= @count
begin
	select a,b from @t where id = @i
	
	set @i = @i + 1
end

[/quote] 没有这么复杂。。 表设计是: 任务id 任务奖励 商品奖励其实去在存储过程的最前面已经获取到了, declare @jiangli nvarchar(50) select @jiangli=col(列明) where 任务id=2 这个时候已经存在了这一行奖励,只不过这个奖励通过函数f_split获取到一个表格,我们是要循环这个表格的。
  • 打赏
  • 举报
回复
你的函数是类似下面的函数把,这样返回了多行值,你要怎么循环处理呢,能说一下不

--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 



select *
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2
/*
任务id	奖励	col
2	6:16,7:23,8:28	6:16
2	6:16,7:23,8:28	7:23
2	6:16,7:23,8:28	8:28
*/
  • 打赏
  • 举报
回复
引用 7 楼 qingYun1029 的回复:
[quote=引用 6 楼 yupeigu 的回复:] 你的函数是类似下面的函数把,这样返回了多行值,你要怎么循环处理呢,能说一下不

--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 



select *
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2
/*
任务id	奖励	col
2	6:16,7:23,8:28	6:16
2	6:16,7:23,8:28	7:23
2	6:16,7:23,8:28	8:28
*/
每一行都是两个数字,这两个数字是传递给另外一个存储过程的两个参数。。 所以每一行都需要调用存储过程。。。 谢谢![/quote] 这样呢:



--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 


declare @t table(id int identity(1,1),a int, b int)
declare @i int
declare @count int

insert into @t
select left(t.col,charindex(':',col)-1),
       SUBSTRING(t.col,charindex(':',col)+1,LEN(t.col))
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2

set @i =1
set @count = (select COUNT(*) from @t)

--循环
while @i <= @count
begin
	select a,b from @t where id = @i
	
	set @i = @i + 1
end

qingYun1029 2013-11-30
  • 打赏
  • 举报
回复
引用 6 楼 yupeigu 的回复:
你的函数是类似下面的函数把,这样返回了多行值,你要怎么循环处理呢,能说一下不

--1.函数
if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf')
   drop function dbo.f_splitSTR
go

create function dbo.f_splitSTR
(
	@split varchar(10),    --分隔字符
	@s varchar(8000)       --要分拆的字符串
	
) 
returns @re table(                      --要返回的临时表
                     col varchar(1000)  --临时表中的列 
                 )
as
begin   
  declare @len int
  
  set @len = LEN(@split)      --分隔符不一定就是一个字符,可能是2个字符
  
  while CHARINDEX(@split,@s) >0
  begin
	insert into @re 
	values(left(@s,charindex(@split,@s) - 1))
	
	set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'')    --覆盖:字符串以及分隔符
  end
  
  insert into @re values(@s)
  
  return   --返回临时表
end
go  

if OBJECT_ID('tb') is not null
   drop table tb
go

create table tb
(
任务id int,
奖励 varchar(1000)
)


insert into tb
select 1,'1:10,2:20,3:25' union all
select 2,'6:16,7:23,8:28' 



select *
from tb
cross apply dbo.f_splitSTR(',',tb.奖励)t
where 任务id = 2
/*
任务id	奖励	col
2	6:16,7:23,8:28	6:16
2	6:16,7:23,8:28	7:23
2	6:16,7:23,8:28	8:28
*/
每一行都是两个数字,这两个数字是传递给另外一个存储过程的两个参数。。 所以每一行都需要调用存储过程。。。 谢谢!
qingYun1029 2013-11-29
  • 打赏
  • 举报
回复
引用 4 楼 hdhai9451 的回复:
先把获取到的数据插入到临时表,再对临时表循环操作, 就看你的数据是否复杂,不复杂有可能不用循环都可以处理.
值只有几行而已,每一行都是 proId:proCount 的形式 话说获取到的table怎么插入临时表呢?求赐教! 谢谢!
Andy__Huang 2013-11-29
  • 打赏
  • 举报
回复
先把获取到的数据插入到临时表,再对临时表循环操作, 就看你的数据是否复杂,不复杂有可能不用循环都可以处理.
qingYun1029 2013-11-29
  • 打赏
  • 举报
回复
当然这个值不会太多,就是奖励的商品不会太多,可能最多也就三四种吧。。
qingYun1029 2013-11-29
  • 打赏
  • 举报
回复
引用 1 楼 yupeigu 的回复:
能不能把表结构提出来,还有你想要的结果
谢谢! 现在在家里,表结构不记得了,只需要一列,我设计的是,表结构是之前设计好的,也没有文档,所以只能根据自己的理解来修改。 任务id,奖励, 奖励我设计的是:projectId:proCount,projectId2:proCount2, 然后查询到时候,根据任务id获取奖励。 select dbo.f_split(',',奖励列);这条语句返回的是一个多行一列的值,然后将值用“:”分割,第一个数字是奖励的商品id,第二个数字是奖励的商品数量。
  • 打赏
  • 举报
回复
能不能把表结构提出来,还有你想要的结果

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧