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)
 Insert to SQL Skipping Duplicates

Author  Topic 

batcater98
Starting Member

22 Posts

Posted - 2008-01-10 : 14:22:01
I have looked at several examples but can not get any of them to work in SQL 2005. I have a Perl Script that is parsing through a flat file loading each row into a database as a record. I am creating a key on the fly by combining some fields from the flat file.

In Perl basically I build the SQL string and simply call the SQL module to do the insert for with the complete SQL string.

My insert string

INSERT INTO fueltemp(tkey,date,time,scan,duration,durday,durhour,durmin,dursec,location)
values ('$tkey','$ndate','$time','$scan','$duration','$durday','$durhour'
,'$durmin','$dursec','$location')";

what do I need to add to this string to check and see if the key $tkey does not already exist in fueltemp?

Thanks,
BatCat.

Regards,
The Dark Knight
-Give What is Right, Not What is Left-

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-01-10 : 14:25:22
what do you want to do with the record to be inserted in case $tkey is already existing in fueltemp?
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2008-01-10 : 14:25:44
Create a proc that checks for the value before inserting and call the proc for each row..
Or dump the rows into a staging table and call a proc that will batch insert only unique rows into the main table.


IF NOT EXISTS (SELECT * FROM YourTable WHERE tKey = @Tkey)
INSERT INTO....



Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

batcater98
Starting Member

22 Posts

Posted - 2008-01-10 : 14:51:30
Thanks - it still gave an error the first time becasue my key contains non alpha characters. But one I put the $tkey in single quotes it went like it should.

Regards,
The Dark Knight
-Give What is Right, Not What is Left-
Go to Top of Page
   

- Advertisement -