?
Solved

How do I use IDENT_CURRENT?

Posted on 2004-10-18
17
Medium Priority
?
4,623 Views
Last Modified: 2012-08-14
Good afternoo!

I need to get the value of the PrimaryKey of the last record entered into a table, without inserting a record. I have tried wriring a stored procedure to do this with 0 success. (you will laugh I am sure!). I am using the result on a web page that is receiving info from the same table. Can I add IDENT_CURRENT into the stored procedure of another querry to get the value?

 CREATE Procedure spGetLastReturnIDFromCMRCRGA
(
@lastreturnID int output
)
AS
SELECT
    @lastReturnID = IDENT_CURRENT
FROM CMRC_Rga
GO
0
Comment
Question by:dplsr
  • 9
  • 7
17 Comments
 
LVL 4

Expert Comment

by:xxg4813
ID: 12340910

Try this:

select IDENT_CURRENT('table_name')


Good luck
0
 
LVL 6

Accepted Solution

by:
JaffaKREE earned 2000 total points
ID: 12340920
This might be useful to you:

IDENT_CURRENT is similar to the Microsoft® SQL Server™ 2000 identity functions SCOPE_IDENTITY and @@IDENTITY. All three functions return last-generated identity values. However, the scope and session on which 'last' is defined in each of these functions differ.

IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
@@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

Try:

CREATE PROCEDURE pGetLastReturnIDFromCMRCRGA ( @MyIdent INT )
as
BEGIN
 DECLARE @MyIdent INT
 Select @MyIdent = Select @@IDENTITY
 RETURN @MyIdent
END
0
 

Author Comment

by:dplsr
ID: 12341218
Hi JaffaKree

I changed your sp around to get rid of a couple of errors. I tessted it in the QueryAnalyzer and it returns null.
and
  on the webpage I get an error: ystem.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'spGetLastReturnIDFromCMRCRGA

CREATE PROCEDURE spGetLastReturnIDFromCMRCRGA (@MyIdent INT output)
as
BEGIN

 Select  @MyIdent = @@IDENTITY
FROM CMRC_RGA
 RETURN @MyIdent
END
GO
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:JaffaKREE
ID: 12341253
you'll get a null if no identity values have been inserted in this session.

try changing it to

select IDENT_CURRENT (TableName)



0
 

Author Comment

by:dplsr
ID: 12341380
Ok success with the Anyalyzer! with:
CREATE PROCEDURE spGetLastReturnIDFromCMRCRGA (@myident int output)
as
BEGIN

 Select @myident =  IDENT_CURRENT('CMRC_RGA')

return  @myident
END
GO

but i am still getting an error: CREATE PROCEDURE spGetLastReturnIDFromCMRCRGA (@myident int output)
as
BEGIN

 Select @myident =  IDENT_CURRENT('CMRC_RGA')

return  @myident
END
GO
0
 

Author Comment

by:dplsr
ID: 12341388
i am still getting error:

System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'spGetLastReturnIDFromCMRCRGA'

could the name be to long? i'll check
0
 
LVL 6

Expert Comment

by:JaffaKREE
ID: 12341421
How is this being called, through ASP ?
0
 

Author Comment

by:dplsr
ID: 12341509
.net

webpage code:
 Dim getlastid As ASPNET.StarterKit.Commerce.ReturnOrdersDB = New ASPNET.StarterKit.Commerce.ReturnOrdersDB()
   Dim lastreturnID as string = getlastId.GetLastReturnIDFromCMRCRGA

and

.vb business object

Public Function  GetLastReturnIDFromCMRCRGA()            
                        Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
            Dim myCommand As New SqlCommand(" spGetLastReturnID", myConnection)      
                        Dim parameterlastReturnID As New SqlParameter("@lastReturnID", SqlDbType.Int, 4)
            parameterlastReturnID.Direction = ParameterDirection.Output
            myCommand.Parameters.Add(parameterlastReturnID)                  
                                  myConnection.Open()
                myCommand.ExecuteNonQuery()      
                         myConnection.Close()
                               Dim lastreturnID As Integer = CInt(parameterlastreturnID.Value)

                Return lastreturnID.ToString()
                        
            end Function
0
 

Author Comment

by:dplsr
ID: 12341587
wwops got to change some variables
0
 

Author Comment

by:dplsr
ID: 12341643
i changed the parameters etc to myindent, but still get the System.Data.SqlClient.SqlException  error
            
            
0
 
LVL 6

Expert Comment

by:JaffaKREE
ID: 12341728
Hmm, not really familiar with .net... shouldn't it be INT though ?
0
 

Author Comment

by:dplsr
ID: 12341943
INT where?
0
 
LVL 6

Expert Comment

by:JaffaKREE
ID: 12341975
Dim lastreturnID as string = getlastId.GetLastReturnIDFromCMRCRGA
0
 

Author Comment

by:dplsr
ID: 12342111
nope acctualy string is correct then you can convert it. I have the same thing on another one that does work returning @@identity after an insert


  Dim addreturninfo As ASPNET.StarterKit.Commerce.ReturnOrdersDB = New ASPNET.StarterKit.Commerce.ReturnOrdersDB()
   Dim returnID as string = addreturninfo.AddReturnInfoToCMRCRGA(strOrderID, strEmail, strfirstname, strlastname)  

strReturnID = cint(ReturnID)
0
 
LVL 6

Expert Comment

by:JaffaKREE
ID: 12342145
Do you need the parameter ?

Dim lastreturnID as string = getlastId.GetLastReturnIDFromCMRCRGA (myIdent)
0
 

Author Comment

by:dplsr
ID: 12342233

nope, cause the parameter is an output . i will award u the points and start a new question  under  asp.net. got to be the way i am calling it.

thanks!
0
 
LVL 6

Expert Comment

by:JaffaKREE
ID: 12342273
Thanks, good luck finishing it off !
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question