Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010
    Posts
    1

    Unanswered: How to get vaue of last row inserted?...

    hey everyone,

    I have a stored procedure--

    CREATE PROCEDURE [dbo].[spOrderNew]

    @CustomerName varchar(250),
    @Address varchar(250),
    @PhoneNo bigint,
    @OrderDate datetime,
    @Pid int,
    @QtyOrdered smallint,
    @Amount int

    AS
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    declare @varCustomerId int
    declare @varOid int

    insert into tblCustomers(CustomerName,Address,PhoneNo)
    values(@CustomerName,@Address,@PhoneNo)
    set @varCustomerId = SCOPE_IDENTITY()

    insert into tblOrder(OrderDate,CustomerId)
    values(@OrderDate,@varCustomerId)
    set @varOid = SCOPE_IDENTITY()

    insert into tblOrderItems(Pid,QtyOrdered,Amount,Oid)
    values(@pid,@QtyOrdered,@Amount,@varOid)
    END

    Now, i am running my first insert query from codebehind and i would remove this query from procedure.How can i get the value of last row inserted into tblCustomers to variable @varCustomerId?...


    Thanks
    Amit

  2. #2
    Join Date
    Jun 2005
    Posts
    319
    SCOPE_IDENTITY() not working? I don't understand the problem.

  3. #3
    Join Date
    Nov 2010
    Posts
    2
    not sure I understand either. But I know you can do "select ident_current('tablename') as value" to get the identity of the last record inserted into that row.

  4. #4
    Join Date
    Jun 2005
    Posts
    319
    select top 1 id from table order by id desc, no?

    better question is why do you need it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •