0

Thank you in advance for your help here.

I want to insert incremental numbers as strings to load some bulk test numbers into a db, here is what i'm using:

Declare 
     @Serialcounter bigint 

set @Serialcounter = 0 

while @Serialcounter < 10 
    insert into [TrackTrace].[dbo].[TAB_ELEMENT] ([Serial], [Batch], [Batch_Id], [QCSample], [StationID]) 
    values(Convert(varchar(60), @Serialcounter), 'test', 8989, 0, 1) 

    set @Serialcounter = (@Serialcounter + 1)

but when I do this it does not increment the counter and I just insert duplicate numbers and do not stop. I think my problem is that my variable is incremented outside of the while loop, but I am not sure how to rectify this.

2
  • 3
    Hi @levi-clouser, where is the begin and the end? Commented Jan 21, 2016 at 6:26
  • thank you, i knew i was missing something basic Commented Jan 21, 2016 at 6:29

2 Answers 2

2
Declare 
@Serialcounter bigint 
set @Serialcounter = 0 
while @Serialcounter < 10 
BEGIN
  PRINT @Serialcounter
    --insert into [TrackTrace].[dbo].[TAB_ELEMENT] 
    --([Serial] 
    --,[Batch] 
    --,[Batch_Id] 
    --,[QCSample] 
    --,[StationID]) 
    --Values(Convert(varchar(60),@Serialcounter),'test',8989,0,1) 
    set @Serialcounter = (@Serialcounter +1 )
END

You not giving begin and end so as for all loops only first statement is considered

Sign up to request clarification or add additional context in comments.

Comments

1

I was missing BEGIN and END statements

DECLARE 
@Serialcounter BIGINT 
SET @Serialcounter = 0 
WHILE @Serialcounter < 10 
BEGIN -- here
    INSERT INTO [TrackTrace].[dbo].[TAB_ELEMENT] 
    ([Serial] 
    ,[Batch] 
    ,[Batch_Id] 
    ,[QCSample] 
    ,[StationID]) 
    VALUES(Convert(varchar(60),@Serialcounter),'test',8989,0,1) 
    SET @Serialcounter = (@Serialcounter +1 )
END    -- and here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.