Solved

SQL Server IDENT_CURRENT

Posted on 2013-06-10
5
571 Views
Last Modified: 2013-06-10
I am trying to find out what my last primary key was on a table, but I am getting the following error.  I need to know that last key so that I can insert a new record with next correct primary key value

SELECT IDENT_CURRENT(dbo.Person)

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "dbo.Person" could not be bound.

nick
0
Comment
Question by:countyprob
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 39235820
Try using single quotes

select ident_current('dbo.Person')
0
 
LVL 1

Author Comment

by:countyprob
ID: 39235941
Thanks Gohord, that worked, except I'm returning a null.  After looking a the table, my last row shows PK_PersonID as 700940.  I was going to use this value to increment by 1 and then insert new value into the table.   Is my approach wrong or is this how a DBA typically inserts the next record?  Sorry for asking dumb question, all my DBA's left.  

nick
0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 39235971
I usually don't use that method but it can work depending on the situation.  If I'm looking for a quick and dirty one time only approach, I'll use that.

Most of the time, I'll take a different approach.  For example, if my code is inserting a record via T-SQL, I'll return the @@identity as a result of a call to an insert stored procedure.  

For example,

declare @myNewId int

insert values into my table

select @myNewId = @@Identity  --this will return the new value of the identity field.

select @myNewId
0
 
LVL 1

Author Closing Comment

by:countyprob
ID: 39235982
Thanks, I appreciate the help
0
 
LVL 10

Expert Comment

by:Banthor
ID: 39236001
I would recommend using SCOPE_IDENTITY() vs @@Identity
because: if there is a trigger on a table that inserts a row on a second table
The second table id value is the @@identity returned.  

IDENT_CURRENT is giving me some very unexpected results.
return 1 for a table with a thousand rows
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 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