Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I use IDENT_CURRENT?

Posted on 2004-10-18
17
Medium Priority
?
4,618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

636 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