Click here to Skip to main content
15,914,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.

i have written an stored procedure(Insert Statement) in sqlserver 2008r2 and i want it to return the last Idcode(this one in my table is Identity) in my table, so i have two problem :

1- what is the difference between these two codes:
SQL
Return @@Identity

and;
SQL
Set Idcode=@@Identity


2- which one of two above statement has to be called?
and where ? i mean inside
SQL
Begin   End

or outside
SQL
Begin   End
??

my query is :

SQL
CREATE PROCEDURE [dbo].[Insert_Person_SP](
@PersonelCode nvarchar(20),
@NonIranPersonId int,
@SodourCityId int,
@CityId int ,
--@MazhabId int,
@GenderId int,
@MilitaryStatusId int,
@MaritialStatusId int,
@IsEmployee bit,
@IsTeacher bit, 
--@JobTitle nvarchar(50),
--@JobDescribtion nvarchar(500),
@LastName nvarchar(60),
@FirstName nvarchar(60),
@FatherName nvarchar(50),
--@NationalityId
@BirthDate_S date,
@SerialNumber nvarchar(50),
@NationalNumber nvarchar(10),
@PostalCode nvarchar(10),
@Phone nvarchar(25),
@Mobile nvarchar(20),
@HomeAddress nvarchar(300),
@Email nvarchar(50)
--@Photo
--@PhotoPath
)
AS
begin
Insert into Persons(
PersonelCode,
NonIranPersonId,
SodourCityId,
CityId,
--MazhabId,
GenderId,
MilitaryStatusId,
MaritialStatusId,
IsEmployee,
IsTeacher,
--JobTitle,
--JobDescribtion
LastName,
FirstName,
FatherName,
--NationalityId
BirthDate_S,
SerialNumber,
NationalNumber,
PostalCode,
Phone,
Mobile,
HomeAddress,
Email
--Photo
--PhotoPath
)
values(
@PersonelCode,
@NonIranPersonId,
@SodourCityId,
@CityId,
--MazhabId,
@GenderId,
@MilitaryStatusId,
@MaritialStatusId,
@IsEmployee,
@IsTeacher,
--JobTitle
--JobDescribtion
@LastName,
@FirstName,
@FatherName,
--NationalityId
@BirthDate_S,
@SerialNumber,
@NationalNumber,
@PostalCode,
@Phone,
@Mobile,
@HomeAddress,
@Email
--Photo
--PhotoPath
)
return @@Identity
end
Posted
Updated 13-Aug-12 21:51pm
v2

Your procedure will work fine. You have to retrieve the ID on your code by having returnvalue parameter.
<br />
<br />
Set Idcode=@@Identity

Above statement means you are assigning the identity to Idcode variable.

@@identity will be defined in between Begin and End

Thanks
Ashish
 
Share this answer
 
Comments
mohammad ehsan 14-Aug-12 4:32am    
thank of your help.
but my IdCode you mentioned which must be returned is Identity and is counted automatically , so i can not insert any thing inside that field .so what shal i do?
AshishChaudha 14-Aug-12 4:52am    
You mean IdCode is identity field in your table?? if you are using @@identity then no need to use Set Idcode=@@Identity. We assign the identity field to a variable when we have to do some calculations using returned identity field.
mohammad ehsan 14-Aug-12 5:10am    
yes, i have IdCode field in my table which is also Indexable,
so you mean it doesn't have any problem for inserting into the IdCode thus it is indexable and count automatically?
AshishChaudha 14-Aug-12 7:56am    
IdCode is your incremented field which is already defined as a primary key, and @@identity only returns a identity field which is generated after successfully insertion in your database.
for example
lCode : Primary Key
there are 2 records in your table and you called a procedure to insert a recored then in your Procedure @@identity will return 3.
set @result==@@Identity
hopes it will works...
give me details your query will hep you out write down this _sp
 
Share this answer
 

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