Click here to Skip to main content
15,891,888 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi everyone
i create table with identity column this column is id ,
and with other column name,adreess so
how i can after inserted get value of this identity column in procedure
SQL
create procedure p_dd(@P_name varchar(40),@P_address  varchar(40),@P_id int output )
as 
begin
insert into mytbel (name,address)
values (@P_name,@P_address)

end 

so there is no way to get value of this column ?
thanks for any hlep
Posted
Updated 9-Jan-12 10:00am
v2

 
Share this answer
 
Comments
Mostafa Elsadany 9-Jan-12 16:01pm    
thanks for help
Wendelius 9-Jan-12 16:07pm    
Exactly, +5 and congratulations on MVP :)
[no name] 9-Jan-12 18:52pm    
Thanks. Same to you
Wonde Tadesse 9-Jan-12 19:44pm    
5+. Congratulation for your MVP
There are several ways of getting the value of newly inserted id. On is as Mark wrote use the scope identity. Your procedure would look something like:
SQL
create procedure p_dd(@P_name varchar(40),@P_address  varchar(40),@P_id int output )
as
begin
   insert into mytbel (name,address)
   values (@P_name,@P_address);

   set @P_id = scope_identity();

end

Another way is to get the actual value from the insert statement using OUTPUT clause. for example:
SQL
create procedure p_dd(@P_name varchar(40),@P_address  varchar(40),@P_id int output )
as
begin
   insert into mytbel (name,address)
   output inserted.id into @P_id
   values (@P_name,@P_address);
end
 
Share this answer
 
Comments
Wonde Tadesse 9-Jan-12 19:44pm    
5+. Congratulation for your MVP
Wendelius 10-Jan-12 0:03am    
Thanks :)

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