1

I need the last inserted row GUID at the time of page loading in ASP.NET. I tried with a stored procedure, but at the time of insertion I am getting how to use that value in the page load procedure like this:

USE [emp]
GO
/****** Object:  StoredProcedure [dbo].[procTestTableInsert]    Script Date: 3/12/2014 5:55:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[procTestTableInsert]
(
    @name varchar(50)
)
AS
    DECLARE @id uniqueidentifier

SELECT @id = NEWID()-- Get a new unique identifier to insert

INSERT 
    testTable1(id,name)
VALUES
    (@id,@name)

SELECT @id  

exec procTestTableInsert sree
Patrick Hofman
  • 148,824
  • 21
  • 237
  • 306
user2693125
  • 11
  • 1
  • 3

1 Answers1

0

According to this question Best way to get identity of inserted row? you should be able to query IDENT_CURRENT.

From the MSDN documentation:

A. Returning the last identity value generated for a specified table The following example returns the last identity value generated for the Person.Address table in the AdventureWorks2012 database.

USE AdventureWorks2012; 
GO
SELECT IDENT_CURRENT ('Person.Address') AS Current_Identity;
GO

Note: This assumes that your GUID is the row ID. If it's not the row ID, this wont work. In which case, I'd recommend either storing the last ID in a temp table in SQL (update your stored proc to save the guid) or have you application cache it.

Community
  • 1
  • 1
Philip Pittle
  • 10,880
  • 7
  • 52
  • 113