1

This question already has an answer here:

I have table test and it has columns id,name and age.I want the id so I am querying like it

select id from test where age=12

After I get the id I am updating the name using update query

Now lets say if age=13 not present then I will insert a row(insert into test(age) values(13)) and after it is inserted I will get the id(as id is autoincremented).So is it possible to do insert and select simultaneously?

marked as duplicate by Paul White says GoFundMonica sql-server Jan 13 '15 at 7:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

So is it possible to do insert and select simultaneously?

Yes, you can use the output clause of the insert statement.

insert into dbo.Test(age) 
output inserted.ID 
values(13); 

Not the answer you're looking for? Browse other questions tagged or ask your own question.