sql嵌套循环
ifexists(select*fromsysobjectswherename='sp_bzjpipei')dropPROCEDUREsp_bzjpipeiuseSCDe...
if exists(select * from sysobjects where name='sp_bzjpipei')
drop PROCEDURE sp_bzjpipei
use SCDecomposition
go
CREATE PROCEDURE sp_bzjpipei
(
@projectid VARCHAR(100)
)
AS
declare @barcode nvarchar(100)
declare @biao nvarchar(100)
declare @a int=0
declare @b int=0
declare @c int=0
declare @d int=0
declare @e int=0
declare @f int=0
declare @i int=0
declare @j int=0
declare @totali int=0
declare @totalj int=0
select @totali=10
while @i<@totali
begin
set @totalj=10
while @j<@totalj
begin
select @i,@j
set @j=@j+1
end
set @i=@i+1
end
只执行了内层循环,外层循环没执行,不知道原因
exec sp_bzjpipei 'abc' 展开
drop PROCEDURE sp_bzjpipei
use SCDecomposition
go
CREATE PROCEDURE sp_bzjpipei
(
@projectid VARCHAR(100)
)
AS
declare @barcode nvarchar(100)
declare @biao nvarchar(100)
declare @a int=0
declare @b int=0
declare @c int=0
declare @d int=0
declare @e int=0
declare @f int=0
declare @i int=0
declare @j int=0
declare @totali int=0
declare @totalj int=0
select @totali=10
while @i<@totali
begin
set @totalj=10
while @j<@totalj
begin
select @i,@j
set @j=@j+1
end
set @i=@i+1
end
只执行了内层循环,外层循环没执行,不知道原因
exec sp_bzjpipei 'abc' 展开
1个回答
2015-06-09
展开全部
@j
这个值没有初始化 第一次循环后 值永远是10 就进不了内循环
要放到 第一个 循环内 去初始
这个值没有初始化 第一次循环后 值永远是10 就进不了内循环
要放到 第一个 循环内 去初始
追问
现在的情况只执行了内层循环,外层循环没执行,及@i=0,@j=0,1,2......9,你可以执行下看看,还是谢谢了
追答
IF EXISTS ( SELECT *
FROM sysobjects
WHERE name = 'sp_bzjpipei' )
DROP PROCEDURE sp_bzjpipei
USE master
go
CREATE PROCEDURE sp_bzjpipei
--(
-- @projectid VARCHAR(100)
--)
AS
DECLARE @barcode NVARCHAR(100)
DECLARE @biao NVARCHAR(100)
DECLARE @a INT= 0
DECLARE @b INT= 0
DECLARE @c INT= 0
DECLARE @d INT= 0
DECLARE @e INT= 0
DECLARE @f INT= 0
DECLARE @i INT= 0
DECLARE @j INT= 0
DECLARE @totali INT= 0
DECLARE @totalj INT= 0
SELECT @totali = 10
WHILE @i < @totali
BEGIN
SET @totalj = 10
SET @j=0
WHILE @j < @totalj
BEGIN
SELECT @i ,
@j
SET @j = @j + 1
END
SET @i = @i + 1
END
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询