Solved

SQL Server - Primary Key/Identity Value Difference

Posted on 2011-09-15
5
269 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In-place Upgrading Dirsync to Azure AD Connect
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
In this video we show how to create an email address policy 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 Mail Flow…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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