一个小问题

jxncwzb 2006-12-11 05:18:45
while(select distinct ClassID from test)--如这里有十条
begin
--在循环时如何取ClassID的值
end
...全文
157 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhzlyg 2006-12-12
  • 打赏
  • 举报
回复

declare @test table( ClassID varchar(10))
insert @test
select '1' union all
select '2' union all
select '3' union all
select '4' union all
select '5' union all
select '6' union all
select '7' union all
select '8' union all
select '9' union all
select '10'

select * from @test


declare @tmp varchar(10)
declare @test2 table( ClassID varchar(10))
declare cur cursor for select * from @test
open cur
fetch cur into @tmp
while (@@fetch_status=0)
begin
insert into @test2 select ClassID from @test
fetch cur into @tmp
end
close cur
deallocate cur

select * from @test2

结果:

表1 test1
ClassID
----------
1
2
3
4
5
6
7
8
9
10

表2 test2
ClassID
----------
1
2
3
4
5
6
7
8
9
10

测试通过...原理是把第1个表的数据classid取出来通过循环插入第2个表中..

_______________________________

回帖是种奉献!
给分是种回报!
欢迎光临:www.sodeer.com
_______________________________
jxncwzb 2006-12-12
  • 打赏
  • 举报
回复
不行呀
cnhzlyg 2006-12-12
  • 打赏
  • 举报
回复
while classid in (select distinct ClassID from test)

这样不行么。

_______________________________

回帖是种奉献!
给分是种回报!
欢迎光临:www.sodeer.com
_______________________________
jxncwzb 2006-12-12
  • 打赏
  • 举报
回复

while(select distinct ClassID from test)--如这里有十条
begin
--在循环时如何取ClassID的值,循环到第一条时取第一条ClassID的值,循环到第二条时取第二条ClassID的值……
end

jxncwzb 2006-12-12
  • 打赏
  • 举报
回复
谢谢,不过test2会出现100条记录,

insert into @test2 select ClassID from @test where ClassID=@tmp//改动

这样行吗
dawugui 2006-12-11
  • 打赏
  • 举报
回复
while classid in (select distinct ClassID from test)
CutBug 2006-12-11
  • 打赏
  • 举报
回复
Declare @id int --申明一个变量
select @id=ClassId from test

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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