往SQL server 里面循环插入数据

mr_cheung 2011-10-02 10:50:15

Create proc Primarytestkey
as
declare @tel int
declare @qq int
set @tel=1
while(@tel<=10)
begin
set @qq=1
while(@qq<=10)
begin
---print @qq
insert into dbo.Uinfo values(@tel,@qq,default)
set @qq=@qq+1
end
set @tel=@tel+1
--print @tel
end



说明:我现在有一张表名为Uinfo ,有两列一列是tel,一列是qq我想往里面循环插入10条数据。按照以上的插入的结果是
QQ TEL
----------- -----------
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
4 1
4 2
4 3
4 4
4 5
4 6
4 7
4 8
4 9
4 10
5 1
5 2
5 3
5 4
5 5
5 6
5 7
5 8
5 9
5 10
6 1
6 2
6 3
6 4
6 5
6 6
6 7
6 8
6 9
6 10
7 1
7 2
7 3
7 4
7 5
7 6
7 7
7 8
7 9
7 10
8 1
8 2
8 3
8 4
8 5
8 6
8 7
8 8
8 9
8 10
9 1
9 2
9 3
9 4
9 5
9 6
9 7
9 8
9 9
9 10
10 1
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 9
10 10

(100 行受影响)
其实我知道是因为set @qq=1 这个地方将QQ又重新赋了一次值才产生的,但是不知道我应该怎样该。
我想要的结果是开始值为1 然后每循环一次+1 直到小于定的那个值。
...全文
276 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
CalvinR 2011-10-09
  • 打赏
  • 举报
回复
给出要的结果
songguangqin 2011-10-09
  • 打赏
  • 举报
回复
楼主需要什么样子的结果啊?
哇咔咔 2011-10-09
  • 打赏
  • 举报
回复
Create proc Primarytestkey
as
declare @tel int
declare @qq int
set @tel=1
while(@tel<=10)
begin
set @qq=1
while(@qq<=@tel)
begin
---print @qq
insert into dbo.Uinfo values(@tel,@qq,default)
set @qq=@qq+1
end
set @tel=@tel+1
--print @tel
end


?
an410398183 2011-10-08
  • 打赏
  • 举报
回复
顶三楼
geniuswjt 2011-10-08
  • 打赏
  • 举报
回复

declare @tel int=1
declare @qq int=1
while(@tel<=10)
begin
insert into Uinfo (tel,qq)
select @tel,@qq,default
set @qq=@qq+1
set @tel=@tel+1
end
黄_瓜 2011-10-08
  • 打赏
  • 举报
回复
while(@tel<=10)
begin
insert into dbo.Uinfo values(@tel,@qq,default)
set @qq=@qq+1
set @tel=@tel+1
end
ben20062143 2011-10-08
  • 打赏
  • 举报
回复
顶4楼
--小F-- 2011-10-07
  • 打赏
  • 举报
回复
请给出所需要的结果
dawugui 2011-10-02
  • 打赏
  • 举报
回复
没搞懂楼主的意思,不知道你需要的结果是什么?
如果是2楼的结果,用一个变量即可,第二个变量没用.

create table uinfo(tel int, qq int)
go

Create proc Primarytestkey
as
declare @tel int
set @tel=1
while(@tel<=10)
begin
insert into Uinfo values(@tel,@tel) --defalut我取消了。
set @tel=@tel+1
end
go

exec Primarytestkey

select * from uinfo

drop proc Primarytestkey
drop table uinfo

/*
tel qq
----------- -----------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

(所影响的行数为 10 行)


*/

怪众生太美丽 2011-10-02
  • 打赏
  • 举报
回复

---要的这样的结果?

QQ TEL
----------- -----------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

(10 行受影响)
怪众生太美丽 2011-10-02
  • 打赏
  • 举报
回复

Create proc Primarytestkey
as
declare @tel int
declare @qq int
set @tel=1
set @qq=1
while(@tel<=10)
begin
insert into dbo.Uinfo values(@tel,@qq,default)
set @qq=@qq+1
set @tel=@tel+1
end

22,300

社区成员

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

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