Hi I am trying to get this to work But it only does the first 135 records.
Can I use Nested loops?
Thanks Daren
declare @.Counter int
declare @.Counter2 int
select @.Counter=1
select @.Counter2=1
while @.Counter < 136
begin
while @.Counter2 < 136
Begin
Insert into dbo.tbl_Matrix (FromID,ToID)
Values (@.Counter,@.Counter2)
set @.Counter2 = @.Counter2 + 1
End
set @.Counter = @.Counter + 1
endHi
Your problem is that you do not reset @.counter2
DECLARE @.Counter int
DECLARE @.Counter2 int
SET @.Counter=1
WHILE @.Counter < 136
BEGIN
SET @.Counter2=1
WHILE @.Counter2 < 136
BEGIN
PRINT '@.counter = ' + CONVERT(varchar,@.counter) + ' @.counter2 = ' +
CONVERT(varchar,@.counter2)
SET @.Counter2 = @.Counter2 + 1
END
set @.Counter = @.Counter + 1
END
John
"Daren Hawes" <newsgroups@.webdesignmagic.com.au> wrote in message
news:eIRcNvnZFHA.3780@.tk2msftngp13.phx.gbl...
> Hi I am trying to get this to work But it only does the first 135
> records.
> Can I use Nested loops?
> Thanks Daren
> --
> declare @.Counter int
> declare @.Counter2 int
> select @.Counter=1
> select @.Counter2=1
> while @.Counter < 136
> begin
> while @.Counter2 < 136
> Begin
> Insert into dbo.tbl_Matrix (FromID,ToID)
> Values (@.Counter,@.Counter2)
> set @.Counter2 = @.Counter2 + 1
> End
> set @.Counter = @.Counter + 1
> end
>|||A table of numbers (table containing every integer from 1 to some
arbitrarily large number) is a handy and much more efficient way to do
the same thing:
INSERT INTO dbo.tbl_Matrix (fromid,toid)
SELECT N1.num, N2.num
FROM Numbers AS N1, tbl_number AS N2
WHERE N1.num BETWEEN 1 AND 135
AND N2.num BETWEEN 1 AND 135
David Portas
SQL Server MVP
--
Wednesday, March 21, 2012
Nested SQL Loops, Possible
Labels:
counter2,
darendeclare,
database,
intdeclare,
loops,
loopsthanks,
microsoft,
mysql,
nested,
oracle,
records,
server,
sql
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment