?
Solved

MSSQL INSERT only if not exists

Posted on 2009-02-19
4
Medium Priority
?
1,151 Views
Last Modified: 2012-06-22
My Table

imei        date             month    
112232   12/21/01    200901

imei = PK

need to insert records into this table.
BUT If imei already exists donot insert
need syntax.....
0
Comment
Question by:isaackhazi
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 2000 total points
ID: 23688673
if not exists (Select * from MyTable Where imei = 112232)
Insert Into Mytable(imei, [Date], [Month])
  Values(112232, '12/21/2001', 200901)
0
 
LVL 8

Author Closing Comment

by:isaackhazi
ID: 31549100
Spot on....
0
 

Expert Comment

by:lazruslong
ID: 27638023
INSERT INTO dbo.ReportUsers (Report_Id, User_Id)
SELECT r2.Report_Id, r2.User_Id
FROM
    dbo.ReportUsers r1 LEFT JOIN
    (SELECT 'rid1' AS Report_Id, 'JohnDoe' AS UserId) r2 ON
        r1.Report_Id = r2.Report_Id AND
        r1.UserId = r2.UserId
WHERE
    s1.Report_Id IS NULL
0
 

Expert Comment

by:lazruslong
ID: 27638048
The above avoids any possibility that a duplicate row could slip in.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question