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 2000 Forums
 Transact-SQL (2000)
 IF Not EXIST or IF EXIST ELSE

Author  Topic 

ASP_DRUG_DEALER
Yak Posting Veteran

61 Posts

Posted - 2004-08-11 : 10:10:35
Hey all-
I need to insert into a table, if a record does not alreay exist. My question is what is better to use or is there an even slicker way?

IF EXIST(SELECT P_ID, P_YEAR, J_ID
FROM PARTS
WHERE P_ID=@PID
AND P_YEAR=@YEAR
AND J_ID=@JOB)
--DO NOTHING, BUT IT WILL NOT SCAN THE ENTIRE TABLE
ELSE
BEGIN
INSERT INTO PARTS(THIS THAT THE OTHER
END
----OR IS THIS ONE BETTER? @ SOME POINT, THERE MAYBE 10K RECORDS IN TABLE---
IF NOT EXIST(SELECT P_ID, P_YEAR, J_ID
FROM PARTS
WHERE P_ID=@PID
AND P_YEAR=@YEAR
AND J_ID=@JOB)
BEGIN
INSERT INTO PARTS(THIS THAT THE OTHER
END

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-11 : 10:17:25
i think that it preety much doesn't matter.

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-08-11 : 13:10:56
Could you just do the INSERT and ignore the error if it already exists? Might be quicker ... particularly if the INSERT will succeed most of the time.

Kristen
Go to Top of Page
   

- Advertisement -