| 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 violationi have a database and one table Table Name :Sample:ID : int Primary keyName :CharPlace:Chariam 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. |
 |
|
|
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. |
 |
|
|
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? |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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, zfrom SourceTable stleft outer joinMyTable mt on my.MyKey = st.MyKeywhere mt.MyKey is NULL =======================================Men build too many walls and not enough bridges. -Isaac Newton, philosopher and mathematician (1642-1727) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-14 : 14:17:43
|
| what if MyKey is not pk of SourceTable? |
 |
|
|
Dennis Falls
Starting Member
41 Posts |
Posted - 2009-09-14 : 14:27:49
|
| INSERT INTO SampleSELECT ID,NAME,Place FROM otherTable tWHERE NOT EXISTS(SELECT * FROM Sample s WHERE s.ID = t.ID) |
 |
|
|
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. |
 |
|
|
|