Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

Check before Insert

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sam - 17 Jul 2008 21:18 GMT
Hi,

I want to check to see if what I want to insert into a table already exists.
I don't want to get an error. Just want to handle this gracefully. In my
example below, even if it's a duplicate, it wouldn't make the entry as both
CompanyID and AddressID are primary key fields but like I said, I don't want
errors. What is the best way to do this?

Here's my INSERT statement
INSERT INTO CompanyAddresses
 (CompanyID, AddressID)
 VALUES (@CompanyID, @AddressID)
Signature

Thanks,

Sam

Aaron Bertrand [SQL Server MVP] - 17 Jul 2008 21:25 GMT
IF EXISTS
(
   SELECT 1
   FROM dbo.CompanyAddress
   WHERE CompanyID = @CompanyID
   AND Address = @AddressID
)
BEGIN
   RETURN;
END
INSERT dbo.CompanyAddress
(
   CompanyID,
   AddressID
)
SELECT
   @CompanyID,
   @AddressID;

On 7/17/08 4:18 PM, in article
3D798A8D-18D5-41B0-83DD-F5539195FDD0@microsoft.com, "Sam"

> Hi,
>
[quoted text clipped - 8 lines]
>   (CompanyID, AddressID)
>   VALUES (@CompanyID, @AddressID)
sloan - 17 Jul 2008 21:26 GMT
if not exists ( select null from CompanyAddresses where CompanyID =
@CompanyID and  AddressID = @AddressID)
begin

INSERT INTO CompanyAddresses
 (CompanyID, AddressID)
 VALUES (@CompanyID, @AddressID)
end

> Hi,
>
[quoted text clipped - 11 lines]
>  (CompanyID, AddressID)
>  VALUES (@CompanyID, @AddressID)
Alex Kuznetsov - 17 Jul 2008 21:43 GMT
> Hi,
>
[quoted text clipped - 12 lines]
>
> Sam

Also consider IGNORE_DUP_KEY option, which allows you to not insert
duplicates without raising an error.
Sam - 18 Jul 2008 02:47 GMT
Thank you all.
Signature

Thanks,

Sam

> > Hi,
> >
[quoted text clipped - 15 lines]
> Also consider IGNORE_DUP_KEY option, which allows you to not insert
> duplicates without raising an error.
Eric Russell - 18 Jul 2008 19:31 GMT
When I'm writing data deployment scripts, I often need to perform both
inserts or updates, depending on if the row already happens to exists on the
target server at the time the script is run in production. I hate duplicating
stuff, so I first insert the data into a table variable. Once done, I can
perform updates and inserts on the target table by joining to the table
variable. It works like a charm and really simplifies the script. Pardon the
pseudo code, but y'all get the idea.

-- First insert data into a table variable:
declare @I table ( a,b,c )

insert into @I ( a,b,c ) select a,b,c
insert into @I ( a,b,c. ) select a,b,c
insert into @I ( a,b,c. ) select a,b,c

-- Next, update rows that do exist:
update T
set T.a = @I.a, ...
from T
join @I on T.pk = @I.pk

-- Finally, insert rows that don't exist:
insert into T ( a,b,c )
select a,b,c
from @I
left join T on T.pk = @I.pk
where T.pk is null

> Hi,
>
[quoted text clipped - 8 lines]
>   (CompanyID, AddressID)
>   VALUES (@CompanyID, @AddressID)
Alex Kuznetsov - 18 Jul 2008 20:03 GMT
On Jul 18, 1:31 pm, Eric Russell
<EricRuss...@discussions.microsoft.com> wrote:
> When I'm writing data deployment scripts, I often need to perform both
> inserts or updates, depending on if the row already happens to exists on the
[quoted text clipped - 40 lines]
>
> > Sam

Also this might be useful:

http://sqlserver-tips.blogspot.com/2006/09/mimicking-merge-statement-in-sql.html
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.