J.Doe
J.Doe

Reputation: 139

Add row to table while incrementing primary key

Say I have a table that looks like:

ID    Name   Age   Co
1     Adls   15    US
2     sldkl  14    FR
3     sldke  16    UK
4     sldee  17    IN

I want to add values to the table and have the ID incremented. ID is the primary key, and I set is Identity under column properties to 'Yes' and identity increment to 1.

So basically, I am doing:

Insert Into TableName(Name, Age, Co)
Values(slkdje, 19, CH)
(sldjklse, 20, AU)
(slfjke, 12, PK)

But, I am getting errors that the primary key is null, and therefore this operation in invalid. How would I add the values, but get the primary key values to increment?

Upvotes: 1

Views: 1436

Answers (1)

Sleepyfalcon
Sleepyfalcon

Reputation: 35

here is a great example for what you want here

here is also a sql query that is copy and paste that will show my example.

create table #temp(
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
)
 insert into #temp(LastName) values('billy'),('bob')
select * from #temp

 drop table #temp;

hope this helps dude.

Upvotes: 1

Related Questions