If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Check for duplicate before insert with composite key

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-03-07, 08:31
darkid darkid is offline
Registered User
 
Join Date: Apr 2007
Posts: 5
Check for duplicate before insert with composite key

Hi,
I'm running into a problem with the following query:

INSERT INTO NetScout.dbo.tb_Ref_App_Link(AppID,LinkID,Date,Vol ume)
SELECT NetScout.dbo.tb_Application.AppID, NetScout.dbo.tb_Link.LinkID, Date, Volume
FROM NetScout.dbo.tb_Application, NetScout.dbo.tb_Link, NetScout.dbo.[OLE DB Destination]
WHERE NetScout.dbo.tb_Application.AppName = NetScout.dbo.[OLE DB Destination].AppName
AND NetScout.dbo.tb_Link.LinkName = NetScout.dbo.[OLE DB Destination].LinkName
AND NetScout.dbo.tb_Application.AppID
NOT IN (SELECT NetScout.dbo.tb_Ref_App_Link.AppID
FROM NetScout.dbo.tb_Ref_App_Link)
AND NetScout.dbo.tb_Link.LinkID
NOT IN (SELECT NetScout.dbo.tb_Ref_App_Link.LinkID
FROM NetScout.dbo.tb_Ref_App_Link)
AND Date
NOT IN (SELECT Date
FROM NetScout.dbo.tb_Ref_App_Link)


I have a composite primary key in my "tb_Ref_App_Link" table, composed of LinkID, AppID, and Date. I want to check if the data i'm trying to insert already exists in the tb_Ref_App_Link table to avoid any referential integrity errors...
How do I say: If LinkID AND AppID AND Date all appear on the same line, then don't insert it...

Any help appreciated.
Reply With Quote
  #2 (permalink)  
Old 05-03-07, 09:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
use a UNIQUE constraint, then you won't have to check, the database will do it for you
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-03-07, 10:06
darkid darkid is offline
Registered User
 
Join Date: Apr 2007
Posts: 5
Ok.
I added a Unique Index Constraint on the 3 columns and now it seems to work...
Thanks!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On