SQL statement to insert record into table that has identity column?

后端 未结 4 1690
忘掉有多难
忘掉有多难 2021-01-24 10:02

I am attempting to insert a row manually into my SQL Server data table. This table has 5 columns, one identity and four data columns. I looked at this post, but when I run the s

4条回答
  •  不思量自难忘°
    2021-01-24 10:39

    Usually, omitting the id column would generate a new automatic ID if the column is set so. In your case, you can use

    SET IDENTITY_INSERT MyTable ON;
    
    INSERT INTO MyTable (id, col1, col2, col3, col4)
    VALUES (4567, 'Value1', 'Value2', 'Value3', 'Value4');
    
    SET IDENTITY_INSERT MyTable OFF;
    

提交回复
热议问题