Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Primary Key Violation

Author  Topic 

nagaraju16
Starting Member

7 Posts

Posted - 2009-09-14 : 10:11:49
Hi frnds,
Good day,
iam strugling with one issue :primary key violation

i have a database and one table
Table Name :Sample:
ID : int Primary key
Name :Char
Place:Char

iam inserting data into the table it is showing tha primary key violation.

i understood that the primary key column will not allow duplicates.
i want to write a query that check the table and insert only the new records and avoide the Duplicates.

how to achive this programeticlly .iam using sql 2005 and c#.net.

plase give me a advice.

thanks,
Nagaraju.




vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-09-14 : 10:16:24
How are you valuing the Primary Key field "ID"?..You may want to define it as an IDENTITY column and be done with it.
Go to Top of Page

nagaraju16
Starting Member

7 Posts

Posted - 2009-09-14 : 10:25:47
Hi vijay,
thanks for ur reply.the ID is not the regular intervals .Example : 1,2,3,4

in my case the ID is random number so i cant put identity for the Column.

Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-09-14 : 10:26:50
but you still haven't answered...how are you valuing the ID column?
Go to Top of Page

nagaraju16
Starting Member

7 Posts

Posted - 2009-09-14 : 10:33:54
ID is Integer values.

and already specified no identity for that coloumn .

raj.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-14 : 11:19:53
didnt understand why you want id to be a random number
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2009-09-14 : 14:15:39
Does this fit your need?

insert into MyTable (MyKey, bing, bang, bong)
select MyKey, x, y, z
from SourceTable st
left outer join
MyTable mt on my.MyKey = st.MyKey
where mt.MyKey is NULL


=======================================
Men build too many walls and not enough bridges. -Isaac Newton, philosopher and mathematician (1642-1727)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-14 : 14:17:43
what if MyKey is not pk of SourceTable?
Go to Top of Page

Dennis Falls
Starting Member

41 Posts

Posted - 2009-09-14 : 14:27:49
INSERT INTO Sample
SELECT ID,NAME,Place FROM otherTable t
WHERE NOT EXISTS(SELECT * FROM Sample s WHERE s.ID = t.ID)
Go to Top of Page

nagaraju16
Starting Member

7 Posts

Posted - 2009-09-15 : 04:13:28
quote:
Originally posted by visakh16

didnt understand why you want id to be a random number



Hi frnd ,
iam getting all information randomply form other sourc in CSV file so i dont want to put the Duplications in my table so i have placed a primary key for the ID.

i will try the other guys mentioned answers.

thanks for all.
raju.
Go to Top of Page
   

- Advertisement -