Answer the question
In order to leave comments, you need to log in
I do Insert in a cycle, the condition of the termination of a cycle is registered, but for some reason does not come to an end?
CREATE PROC sp
AS
DECLARE @countrows int
SET @countrows = (select count(*) from таблица)
BEGIN
insert into таблица (строка) values ( rand() )
WHILE
@countrows < 20
IF @countrows > 20
BREAK
ELSE
CONTINUE
END
Answer the question
In order to leave comments, you need to log in
Well, why is it not limited to 20 - this is generally on the surface, the check is outside the cycle, but should be in it.
But to do so is not right - count is a rather heavy operation, and if, in addition, there are thousands of lines ... Declare a
variable and increment it
DECLARE @i INT = 0;
here is the
if @i > 20 loop -- here is the check
set @i = @i + 1; -- here increase
And any jerking of the server once again. This is the first moment.
The second point - if you just need to fill the table with test data, then there are solutions that not only do this, but also generate something similar to real data, it's more convenient, IMHO. The same ToolBelt can be viewed.
CREATE PROC sp
AS
DECLARE @countrows int
SET @countrows = (select count(*) from таблица)
BEGIN
insert into таблица (строка) values ( rand() )
WHILE
@countrows < 20
SET @countrows = (select count(*) from таблица)
IF @countrows > 20
BREAK
ELSE
CONTINUE
END
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question