Search code examples
sqlsql-serverdatabasesql-insert

Add values to an identity(1,1) column in SQL Server


I have two tables:

CREATE TABLE project.teacher 
(
    PROFESSOR_Codigo SMALLINT IDENTITY( 100, 1),
    birth            DATE NOT NULL,
    phone            VARCHAR(15)
);

CREATE TABLE project.student
(
    STUDENT_Codigo IDENTITY( 1, 1),
    birth          DATE NOT NULL,
    phone          VARCHAR(15)
);

When I add a student to the student table it will increment 1 from the number 1, and stay (1, 2, 3, 4, 5, 6, ...)

In the teacher table I already have data entered, and start with a value of 100 in the identity column, but when I add 1, it gets identity 1, not 101

I've tried everything, scope_identity(), @@identity, but I couldn't! Does anyone have any ideas?

What I do to insert a row into the teacher table:

INSERT INTO project.student 
VALUES ('1998-05-08', '963597461');

INSERT INTO project.teacher
VALUES ('1994-05-09', '968413692');

Solution

  • try

    dbcc checkident(teacher, noreseed)
    

    this will return the current identity value of the ident column. your create statement is fine so maybe something went wrong on the on create?