154

My questions is

how to increment a column's value by 1.

For example, suppose a column ID has values 1,2,3,4, ..

Now when I update this table then ID column should increment by 1,

Now ID will become 2,3,4,5, ..

petra
  • 2,642
  • 2
  • 20
  • 12
Varinder
  • 1,780
  • 2
  • 11
  • 18
  • any sample about you want ? increment column's value by 1 for 1 row ? all rows ? which is your table (DDL) ? data samples (DML) ? – Kiquenet Aug 19 '16 at 09:18

8 Answers8

239

To add one to every value in the table...

UPDATE myTable
SET ID = ID + 1

To create a new value, one more then the previous highest (usually), use a column with IDENTITY

gbn
  • 422,506
  • 82
  • 585
  • 676
  • 63
    Please note that this does _not_ work when the column is NULL. NULL columns stay NULL, even after incrementing. For this to solve, use the [`ISNULL` statement](http://msdn.microsoft.com/en-us/library/ms184325.aspx) like this: `UPDATE myTable SET ID = ISNULL(ID, 0) + 1` (Taken from [this SO answer](http://stackoverflow.com/a/9136574/107625)) – Uwe Keim Jun 09 '14 at 08:09
  • `SET [Lic] = [Lic] + @dif` . Note ***@dif*** variable can be _positive, 0, or negative_ – Kiquenet Aug 19 '16 at 09:17
59

If you want to have an unique number for each row automatically generated, this is IDENTITY as per Neil's answer.

If each time you update the table you want to increase the values (ie they are not keys):

Update MyTable
Set IDColumn = IDColumn + 1
Where <whatever>
kaj
  • 5,133
  • 2
  • 21
  • 18
10

Try this:

Update Emp set testCount = ISNULL(testCount, 0) + 1 where testId=1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
kishor soneji
  • 795
  • 1
  • 9
  • 16
6

Update on the accepted answer

Might be shorter having it like this.

UPDATE myTable
SET ID += 1
WHERE <Condition>

Tinashe Makuti
  • 131
  • 1
  • 3
3

For postgresSQL

UPDATE myTable SET ID = COALESCE(id, 0) + 1;
1

You could try the following:

DECLARE @i INT
SET @i = @@ROWCOUNT + 1

INSERT INTO YourTable
        (Identity Column)    
VALUES    
        (@i + 1)
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
0

In Oracle the code is a little bit more tricky.

You will have to create an auto-increment field with the sequence object (this object generates a number sequence).

Use the following CREATE SEQUENCE syntax:

CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10

The code above creates a sequence object called seq_person, that starts with 1 and will increment by 1. It will also cache up to 10 values for performance. The cache option specifies how many sequence values will be stored in memory for faster access.

To insert a new record into the "Persons" table, we will have to use the nextval function (this function retrieves the next value from seq_person sequence):

INSERT INTO Persons (ID,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen')

The SQL statement above would insert a new record into the "Persons" table. The "ID" column would be assigned the next number from the seq_person sequence. The "FirstName" column would be set to "Lars" and the "LastName" column would be set to "Monsen".

Sandeep
  • 1,504
  • 7
  • 22
  • 32
piyushj
  • 1,546
  • 5
  • 21
  • 29
  • SQL Server now has support for this too, Sequences is a superior solution to doing the same thing manually. https://www.sqlservertutorial.net/sql-server-basics/sql-server-sequence/ – Chris Schaller Feb 14 '21 at 03:16
0

You can use IDENTITY which will do this for you.

CREATE TABLE [dbo].[MyTable](
    [MyTableID] [int] IDENTITY(1,1) NOT NULL,
    -- Other columns
)

When you insert your first record, you'll get an Id of 1.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
  • The question is about updating a column in an existing row, not about incrementing an ID when inserting new rows. – Barmar Feb 11 '21 at 16:40
  • But @Barmar it is a good observation worth raising with OP, it looks like using `IDENTITY` might have solved this problem in a different way in the first place, given that OPs number might be the reverse of the incrementing identity. – Chris Schaller Feb 14 '21 at 10:50