Solved

SQL Server - Primary Key/Identity Value Difference

Posted on 2011-09-15
5
270 Views
Last Modified: 2012-05-12
Hi

I have a stored procedure that inserts some data into a table, it then returns @@Identity of the inserted record and updates another table with that value, and it links them together

However, I made the mistake of forgetting to make the field a primary key, and i wanted to put a foreign key on another table. So i have made it one. Now though, the @@Identity that is returned, does not match the value that is actually in the field - how can I get the 2 to correlate ?

I hope this makes sense !

Thanks
0
Comment
Question by:seancurt
[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
5 Comments
 
LVL 4

Expert Comment

by:rbride
ID: 36541763
Let me get this straight: you have declared a column with the INDENTITY property and when you insert to the table via the SP you read back the last inserted identity value through @@IDENTITY.

It is no problem to add primary / foreign key constraints afterwards: just use the appropriate ALTER TABLE command.

If you are using an IDENTITY field in the second table, that might be tripping you up because @@IDENTITY is the last inserted value on whatever table.

drop table t
go
drop table tfk
go
create table t (
      id int PRIMARY KEY IDENTITY,
      d varchar(10)
)
go
create table tfk (
      id int,
      t_id int FOREIGN KEY REFERENCES t (id),
      d varchar(10)
)
go


DECLARE @ID INT
INSERT t (d) VALUES ('abc')

SELECT @ID = @@IDENTITY
SELECT @ID, * FROM t

INSERT tfk VALUES (1, @ID, 'def')
go

select * from tfk
go
 

Note: it is not really considered good practice to use the identity column as a primary key but for some circumstances it is pragmatic.
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 36542384
Primary key does not accept duplicate value

Identity is the running value you can force fully change the start position of indentity and it will accept duplicates if you have changed starting point.

create table #temptable  (id int identity(1,1), varc varchar(10))
insert into #temptable (varc) values ('Alpesh')
insert into #temptable (varc) values ('Alpesh1')
insert into #temptable (varc) values ('Alpesh2')
Set Identity_insert #temptable ON
insert into #temptable (id,varc) values (2, 'Alpesh3')
Set Identity_insert #temptable OFF
insert into #temptable (varc) values ('Alpesh4')
select * from #temptable

Open in new window

0
 

Author Comment

by:seancurt
ID: 36542799
ahhhh. rbride you have made me think now !

I added a trigger to the database, that inserts to another table, so it will be the PK of that table that is being returned.

Hmmm. So now to get the ID of the other table
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36543099
It looks like the Experts here already are helping you, but I will chime in only to say I would go with SCOPE_IDENTITY() over @@IDENTITY. You can try, SCOPE_IDENTITY() might work if the TRIGGER insert is treated as being in a different scope. http://msdn.microsoft.com/en-us/library/ms190315.aspx
0
 

Author Closing Comment

by:seancurt
ID: 36547940
Scope_Identity was the one, thanks !
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

628 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