I have a table with a primary key, two foreign keys and other attributes. I want to insert a row in a way that just after insertion it should return the PRIMARY KEY, I am using following statement to execute query
int MyId = (int)insert.ExecuteScalar();
but above code is returning one of foreign keys, which is part of insert query. How can I get primary key in return after insertion? Second, is there any way to get any specific attribute just after insertion.
I am using asp.net and Sql Server 2008
use output !!
With a table like this in SQL Server:
You can use SCOPE_IDENTITY(): (error checking left out, etc)
But honestly I would recommend if at all possible you use an abstraction (Linq2SQL, Entity Framework, NHibernate, etc, etc) for this kind of thing because it frees you from having to deal with this kind of boilerplate.