Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to inert values to a table and return its primary key and a column value during insertion
Posted

You can return the @@IDENTITY variable (note this only works for the last inserted value i.e. you can't insert multiple rows and return all the row numbers):
SQL
insert into tablename (...) values (...)
select @@identity
 
Share this answer
 
Comments
Sharon 2 1-Apr-13 2:16am    
how can return 2 rows from table in insertion procedure
Mehdi Gholam 1-Apr-13 2:21am    
You can write select @@identity after each insert.
this way...
SQL
declare @return1 int;
set @return1=0; 
insert into table1(col1,col2) values('val1','val2'); 
select @return1=@@Identity;

insert into table1(col1,col2) values('val1','val2');
select @return1 as Insert1_PKey_Val, @@Identity as Insert2_PKey_Val;


it is store procedure then you can also use scope_identity function after insert statement
SQL
select Scope_Identity()

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Sharon 2 1-Apr-13 2:16am    
how can return 2 rows from table in insertion procedure
Aarti Meswania 1-Apr-13 2:19am    
didn't get it
can u give example that clear your requirement?
Sharon 2 1-Apr-13 2:17am    
means 2 column vale
Aarti Meswania 1-Apr-13 2:30am    
check updated solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900