Solved

How do I use IDENT_CURRENT?

Posted on 2004-10-18
17
4,593 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 500 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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