求帮忙看一下错误在哪

lakerskobemvp 2014-06-23 09:48:31

CREATE TABLE AB(
[a] int,
[b] int
)
GO

declare @a int
set @a = 1
declare @b int
set @b = 1
while @a < 10
begin
while @b<10
begin
insert into AB values
(@a,@b)
set @b+=1
end
set @a+=1
end

GO
select * from AB

本来想笑得结果是
1 1
1 2
1 3
.
.
.
1 9
2 1
.
.
2 9
.
.
.
9 1
.
.
.
9 9
可是结果只有
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
是不是循环写错了,求指点


...全文
101 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen357313771 2014-06-23
  • 打赏
  • 举报
回复
额,搞错了,@b第一次循环完后是10,所以外循环第二次进去的时候,里边就不会再循环了,所以不会再插入
引用 3 楼 chen357313771 的回复:
因为第二个while第一次循环完后变成了9,然后外边开始第二次,这时候不给set 1 的话,@b 还是9 [quote=引用 2 楼 lakerskobemvp 的回复:] [quote=引用 1 楼 chen357313771 的回复:]
CREATE TABLE #AB(
    [a] int,
    [b] int
) 
GO
 
declare @b int
declare @a INT
set @a = 1
set @b = 1
	while @a < 10
	BEGIN
		SET @b=1  
		while @b<10
		begin
		insert into #AB values
		(@a,@b)
		set @b+=1
		end
	set @a+=1
	end
 
GO
select * from #AB 
DROP TABLE #AB
成功了,我想知道在循环中为什么要写一下SET @b=1 ,求解答[/quote][/quote]
以学习为目的 2014-06-23
  • 打赏
  • 举报
回复
DECLARE @AB TABLE (
    [a] int,
    [b] int
) 

DECLARE  @a INT,@b int
SET   @a = 1
WHILE @a<=9
  BEGIN
     SET @b=1
    WHILE @b<=9
BEGIN
INSERT INTO  @AB VALUES
(@a,@b)
SET @b=@b+1
END 
SET @a=@a+1
END

SELECT *  FROM @AB
chen357313771 2014-06-23
  • 打赏
  • 举报
回复
因为第二个while第一次循环完后变成了9,然后外边开始第二次,这时候不给set 1 的话,@b 还是9
引用 2 楼 lakerskobemvp 的回复:
[quote=引用 1 楼 chen357313771 的回复:]
CREATE TABLE #AB(
    [a] int,
    [b] int
) 
GO
 
declare @b int
declare @a INT
set @a = 1
set @b = 1
	while @a < 10
	BEGIN
		SET @b=1  
		while @b<10
		begin
		insert into #AB values
		(@a,@b)
		set @b+=1
		end
	set @a+=1
	end
 
GO
select * from #AB 
DROP TABLE #AB
成功了,我想知道在循环中为什么要写一下SET @b=1 ,求解答[/quote]
lakerskobemvp 2014-06-23
  • 打赏
  • 举报
回复
引用 1 楼 chen357313771 的回复:
CREATE TABLE #AB(
    [a] int,
    [b] int
) 
GO
 
declare @b int
declare @a INT
set @a = 1
set @b = 1
	while @a < 10
	BEGIN
		SET @b=1  
		while @b<10
		begin
		insert into #AB values
		(@a,@b)
		set @b+=1
		end
	set @a+=1
	end
 
GO
select * from #AB 
DROP TABLE #AB
成功了,我想知道在循环中为什么要写一下SET @b=1 ,求解答
chen357313771 2014-06-23
  • 打赏
  • 举报
回复
CREATE TABLE #AB(
    [a] int,
    [b] int
) 
GO
 
declare @b int
declare @a INT
set @a = 1
set @b = 1
	while @a < 10
	BEGIN
		SET @b=1  
		while @b<10
		begin
		insert into #AB values
		(@a,@b)
		set @b+=1
		end
	set @a+=1
	end
 
GO
select * from #AB 
DROP TABLE #AB

34,587

社区成员

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

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