Solved

How do I use IDENT_CURRENT?

Posted on 2004-10-18
17
4,580 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now