存储过程循环表的一列数据

yang2028 2011-01-10 03:06:55
存储过程循环表里的一列数据,速度,效率高
如A表里有3列把第二列数据循环出来
...全文
192 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
長胸為富 2011-01-10
  • 打赏
  • 举报
回复

--游标
declare @b varchar(10)
declare tempcur cursor
for select b from tb
open tempcur
fetch next from tempcur into @b
while @@fetch_status = 0
begin
print @b
fetch next from tempcur into @b
end
close tempcur
deallocate tempcur

-晴天 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yang2028 的回复:]
引用 6 楼 qianjin036a 的回复:
SQL code
create table tb(a varchar(10),b varchar(10),c varchar(10))

朋友,这个问题必须用到whlie循环,在循环里还要取到B列的没一个数据进行处理
……
[/Quote]
你觉得这样处理不是获得b 列的所有值吗?
create table tb(a varchar(10),b varchar(10),c varchar(10))
insert into tb select 'k','a','q'
insert into tb select 'k','d','q'
insert into tb select 'k','e','q'
go
select distinct replace(b,' ','') from(
select stuff((select ' '+b from tb for xml path('')),1,1,'') b from tb
)t
go
drop table tb
/*
ade

(1 行受影响)

*/
华夏小卒 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yang2028 的回复:]
引用 6 楼 qianjin036a 的回复:
SQL code
create table tb(a varchar(10),b varchar(10),c varchar(10))
insert into tb select 'a','a','a'
insert into tb select 'd','d','d'
insert into tb select 'e','e','e'
……
[/Quote]怎么处理?讲讲,为了效率,尽量少用循环
yang2028 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qianjin036a 的回复:]
SQL code
create table tb(a varchar(10),b varchar(10),c varchar(10))
insert into tb select 'a','a','a'
insert into tb select 'd','d','d'
insert into tb select 'e','e','e'
go
select distinct repl……
[/Quote]


朋友,这个问题必须用到whlie循环,在循环里还要取到B列的没一个数据进行处理
-晴天 2011-01-10
  • 打赏
  • 举报
回复
create table tb(a varchar(10),b varchar(10),c varchar(10))
insert into tb select 'a','a','a'
insert into tb select 'd','d','d'
insert into tb select 'e','e','e'
go
select distinct replace(b,' ','') from(
select stuff((select ' '+b from tb for xml path('')),1,1,'') b from tb
)t
go
drop table tb
/*
ade

(1 行受影响)

*/
yang2028 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qianjin036a 的回复:]
SQL code
select B列 from 表1
[/Quote]


用whlie 循环
-晴天 2011-01-10
  • 打赏
  • 举报
回复
select B列 from 表1
yang2028 2011-01-10
  • 打赏
  • 举报
回复
表1
A列 B列 C列
a a a
d d d
e e e


把B列ade 打印出来
-晴天 2011-01-10
  • 打赏
  • 举报
回复
啥叫"循环出来"?
不明白.

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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