I have a column in my database. How do i insert an incremented number via an insert so it fills it every row?
- 3add auto increment for that columnFathah Rehman P– Fathah Rehman P2013-08-02 09:02:33 +00:00Commented Aug 2, 2013 at 9:02
- Hope you will get one of useful solution using [Increment Sequence Number on Sql Insert][1] [1]: stackoverflow.com/questions/15530222/…Twinkle– Twinkle2013-08-02 09:17:39 +00:00Commented Aug 2, 2013 at 9:17
6 Answers
Use Identity column
Suppose you want Column Id to be incremented automatically a row is inderted define it as identity
ID INT IDENTITY (1,1) First 1 is starting value second 1 is seeding it means increment by which integer
in this case the value will start at 1 and increment by 1 every time u insert a new row.
Please let me know if any further help needed
Comments
You may go to the designer of the table add a new Column and then go to the properties tab for the column and set
Identity Specification
- IsIdentity :Yes
- Identity Increment : 1
- Identity Seed : 1
Identity Increment sets the number that will be added each time you insert a row. If it was 10 then you would have ids like 10, 20, 30.
Identity Seed is an offset you may need to add (which is the first number to appear) If it was 10 then your first Id would be 10.
Comments
Create auto increment for that column for example
CREATE TABLE table_name ( id int NOT NULL IDENTITY (1, 1), name varchar(50) NULL ) ON [PRIMARY] In the above example column id is auto increment. After each insert its value will increment by 1.
If you want to change that increment number later, please check