Skip to content
Advertisement

How to increment a primary key in an insert statement in SQL Server 2005

I need to write an insert statement into a table the columns looks like this

  • demandtypeid (PK, FK, int, not null)
  • characvalueid (PK, FK, int, not null)
  • percentage (int null)
  • lastuser (varchar(100), null)
  • lastedited (datetime, null)

Here is the INSERT statement. Notice the there is not values at the

as I think that’s where the auto-increment command should go

Please help with a simple little statement

I just want to know how to manually insert into this table

Here’s my table structure:

Advertisement

Answer

Given the CREATE TABLE statement you posted, without auto-increment (aka identity) columns, you would insert providing all columns and values, like this:

If, however, you do make them auto-increment by changing the CREATE TABLE to:

Then you would insert providing all non-identity (non-autoincrement) columns like this:

However, it is not common to have more than one column as an identity (autoincrement) column, and generally, this column is the only PRIMARY KEY column.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement