Insert manually into a table by SQL statement, but key is autoincremented

前端 未结 4 2140
北荒
北荒 2020-12-17 15:29

Let\'s say I have a table of users and the id column is the primary key and auto incremented.

I want to just try and add user manually by this statemen

4条回答
  •  悲哀的现实
    2020-12-17 16:11

    If id is a auto incremented value just leave that column out of the insert and it will fill in with the auto incremented value. For example from BOL http://msdn.microsoft.com/en-us/library/aa933196(v=SQL.80).aspx

    CREATE TABLE new_employees
    (
     id_num int IDENTITY(1,1),
     fname varchar (20),
     minit char(1),
     lname varchar(30)
    )
    
    INSERT new_employees
       (fname, minit, lname)
    VALUES
       ('Karin', 'F', 'Josephs')
    
    INSERT new_employees
       (fname, minit, lname)
    VALUES
       ('Pirkko', 'O', 'Koskitalo')
    

提交回复
热议问题