Click here to Skip to main content
15,891,017 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I try this


I have reslult
1
1
1
..

Need to have
1
2
3
..

What I have tried:

<pre>declare @maxNo integer = 0 select @maxNo = isnull(max(redni_broj), 0) from [dbo].[prenosnica_roba] WHERE id_fakture=3;Set @maxNo=@maxNo+1;
INSERT into dbo.prenosnica_roba (redni_broj)
     SELECT  @maxNo
    FROM dbo.mp_racun_roba 
Posted
Updated 19-Nov-19 23:42pm

You probably can use one of the ranking functions of SQL for that...
Ranking Functions (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
You set @MaxNo to the current max value from your table - but you don't change that value. So the first time it finds a value, it will duplicate it ad infinitum. Probably, you want to increment @MaxNo before you insert - but that's not a good idea.
Almost certainly, what you actually want to do is to use an IDENTITY value instead, and lest the DB sort it out for you: IDENTITY (Property) (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 

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