Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert a sequence number into a table using sql but it is only inserting one record

What I have tried:

alter PROCEDURE NumGen
(
@Numgen BIGINT
) 

AS
DECLARE @Counter BIGINT
SET @Counter = 1000

WHILE @Counter = @Numgen
BEGIN 
INSERT INTO dbo.NumberGen
(
    Num
)
SELECT  @Counter
	SET @Counter = @Counter + 1 ;
END 

SELECT * FROM dbo.NumberGen
Posted
Updated 15-Aug-18 3:07am
Comments
Herman<T>.Instance 15-Aug-18 8:47am    
WHILE @Counter = @Numgen ...... WHILE @Counter < @Numgen ??

SET @Counter = 1000 ???

What are you trying?
MarkNinja 15-Aug-18 9:26am    
i am trying to do this

116049610000000
116049610000001
116049610000002
116049610000003
116049610000004
116049610000005
116049610000006
116049610000007
116049610000008
116049610000009
116049610000010
116049610000011
116049610000012
116049610000013
116049610000014
116049610000015

1 solution

Look at your code:
SQL
SET @Counter = 1000
WHILE @Counter = @Numgen
BEGIN  
   ...
   SET @Counter = @Counter + 1 ;
END
It will only insert one row, if you start by asking for a thousand...
Perhaps <, <=, >=, or > would be better?
 
Share this answer
 
Comments
Herman<T>.Instance 15-Aug-18 9:29am    
indeed a mix up of variables to use and good logic

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900