all groups > sql server programming > june 2005 >
You're in the

sql server programming

group:

Insert Into Where Not Exists


Insert Into Where Not Exists Mike Labosh
6/22/2005 8:58:16 PM
sql server programming:
In my application, I have an ADO.NET DataTable whose schema is the same as
this table:

CREATE TABLE dbo.CMRPhone (
CLMRKey INT NOT NULL,
PhoneNumber NVARCHAR(40) NOT NULL
)

The PK of the table is a composite of both columns.

The items in the ADO.NET DataTable may or may not have duplicates. I am
using an ADO.NET SqlDataAdapter's Update method to insert records into the
db table. Internally, the SqlDataAdapter iterates over the items in my
DataTable and calls my insert sp once for each record, so the SP must be
phrased to insert one record, given the two parameters:

CREATE PROCEDURE dbo.LoadCMRPhones (
@clmrKey INT,
@phoneNumber NVARCHAR(40)
) AS
INSERT INTO CMRPhone
SELECT @clmrKey, @phoneNumber
WHERE NOT EXISTS (
SELECT @clmrKey, @phoneNumber
FROM CMRPhone
)
GO

But I don't like using sub-selects. Anyone know of a more efficient way of
phrasing this insert?

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"

Re: Insert Into Where Not Exists Mike Labosh
6/22/2005 11:14:05 PM
[quoted text, click to view]

Therein lies my indecision.

The "corporate culture" here is one of vast "sloppitude". They have years
and years worth of VBA and VB6 code that does stuff like this:

On Error Resume Next
' Insert a bazillion items with massive duplications to a unique set of
columns
' go merrily on your way

And I am trying to discipline them to not do that.

The sub-select works, and has a decent looking execution plan, I just have
an irrational distaste of the sub-select, and avoid it when I can. I was
just here polling to see if anyone had other techniques. I understand that
your comments will also work, but they do WAY TOO MUCH of that sort of thing
here already.

[quoted text, click to view]

I noticed that was using a table scan, but in my testing, there's only a few
rows in the table, so it could be legitimate that the optimizer chose a
scan. Perhaps I should run a few life-size batches in the morning.

Thanks for the input.
--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"

Re: Insert Into Where Not Exists Mike Hodgson
6/23/2005 12:00:00 AM
If you *really* don't like subqueries and you have a primary key
constraint on those 2 columns, then you could always just do the insert
(without the WHERE clause) and ignore/suppress any errors that occur as
a result of violating the PK constraint. The constraint would take care
of rolling back the transaction so the dup row does not get inserted
into the table.

The EXISTS() predicate is usually pretty good (as it stops the index
scan as soon as it finds a matching row), although you picked the one
case where it will most likely scan the entire index, that is when there
is no matching row (i.e. the row you're inserting is not a dup).

--
*mike hodgson*
/ mallesons stephen jaques/
blog: http://sqlnerd.blogspot.com



[quoted text, click to view]
RE: Insert Into Where Not Exists Damien
6/23/2005 3:54:02 AM
I tend to break my sps up with some validation at the top: eg

-- Check record doesn't already exist
IF EXISTS ( SELECT * FROM CMRPhone WHERE CLMRKey = @clmrKey AND PhoneNumber
= @phoneNumber )
BEGIN
RAISERROR( 'Record already exists with CLMRKey %i and PhoneNumber %s.',
16, 1, @clmrKey, @phoneNumber )
RETURN
END

-- Add new record
INSERT INTO CMRPhone
SELECT @clmrKey, @phoneNumber


So if the sp fails, it's clear where it happened.


Damien

[quoted text, click to view]
AddThis Social Bookmark Button