Why does this line not work

It says that the command completed successfully, but there are no values ​​inserted into the organization table.

DECLARE @now DATETIME
SET @now = GETDATE()
DECLARE @numtoinsert INT
SET @numtoinsert = 100

DECLARE @counter INT
SET @counter = 101
WHILE @counter < @numtoinsert
BEGIN
SET @counter = @counter + 1

INSERT INTO [MVServices].[dbo].[Organization]
       ([Organization_Id]
       ,[Business_Number]
       ,[Legal_Name]
       ,[Common_Name]
       ,[Operating_As]
       ,[Sort_Name]
       ,[Effective_Date]
       ,[Expiry_Date]
       ,[Created_By]
       ,[Created_Date]
       ,[Last_Changed_By]
       ,[Update_Date])
 VALUES
       (@counter
       ,1234
       ,'ABC Construction'
       ,'ABC'
       ,'ABC Construction'
       ,'ABC Construction'
       ,@now
       ,null
       ,'seed'
       ,@now
       ,null
       ,null)

       END
+3
source share
4 answers

It does not insert anything, because your condition is WHILE:

WHILE @counter < @numtoinsert 

And 101> 100, so it never enters the cycle.

+9
source

This is because you are inserting records so far @counter < @numtoinsert, but as @counterequal to 101, it is never less @numtoinsert, which is 100.

+3
source

:

SET @numtoinsert = 100
SET @counter = 101 

while:

WHILE @counter < @numtoinsert

numtoinsert , insert .

+2

:

SET @numtoinsert = 100
SET @counter = 101

WHILE @counter < @numtoinsert

:

WHILE 101 IS SMALLER THAN 100

false, while.

+1

All Articles