Zbiebu
asked on
Return ID to user
Hi,
How would I be able to return the value of an identity field to the user after inserting data.
Thanks
How would I be able to return the value of an identity field to the user after inserting data.
Thanks
don't use @@identity it doesn't return the value you desire ...
you need to use the scope_identity() function instead
for all sql server since sql 2000
declare @id int,@err int,@rows int
insert into yourtable (column list)
select .....
select @err=@@error,@rows=@@rowco unt,@id=sc ope_identi ty()
the identity value of the last row inserted by the previous insert statement in this scope will be retrieved...
this protects you from the actions of any triggers , which maybe invoked by the statement...
another method is to use the OUTOUT clause (which is a MS SQL extension)
you need to use the scope_identity() function instead
for all sql server since sql 2000
declare @id int,@err int,@rows int
insert into yourtable (column list)
select .....
select @err=@@error,@rows=@@rowco
the identity value of the last row inserted by the previous insert statement in this scope will be retrieved...
this protects you from the actions of any triggers , which maybe invoked by the statement...
another method is to use the OUTOUT clause (which is a MS SQL extension)
ASKER
ALTER PROCEDURE [dbo].[usp_InsertDetails]
@Title VARCHAR(500),
@Description VARCHAR(255),
@Section VARCHAR(50),
@CompiledBy VARCHAR(20),
@Link VARCHAR(50),
@Version INT,
@Date SMALLDATETIME
AS
SET NOCOUNT ON;
SET @Version = 1
INSERT INTO Details
(Title, [Description], Section, CompiledBy,Link, Version,Date)
VALUES(@Title, @Description, @Section, @CompiledBy, @Link, @Version,@Date)
Hi Thanks for the reply.
Above is the code for the SP for insert, where would I place the code you mention.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
create proc(@Ident int out )
AS
BEGIN
insert into table ....
SET @Ident = @@IDENTITY
END