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 直到小于定的那个值。
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
---要的这样的结果?QQ TEL
----------- -----------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10(10 行受影响)
如果是2楼的结果,用一个变量即可,第二个变量没用.create table uinfo(tel int, qq int)
goCreate 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
goexec Primarytestkeyselect * from uinfodrop 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 行)
*/
begin
insert into dbo.Uinfo values(@tel,@qq,default)
set @qq=@qq+1
set @tel=@tel+1
end
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
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
?