MSSQL INSERT only if not exists

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.....
LVL 8
isaackhaziAsked:
Who is Participating?
 
Daniel WilsonCommented:
if not exists (Select * from MyTable Where imei = 112232)
Insert Into Mytable(imei, [Date], [Month])
  Values(112232, '12/21/2001', 200901)
0
 
isaackhaziAuthor Commented:
Spot on....
0
 
lazruslongCommented:
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
 
lazruslongCommented:
The above avoids any possibility that a duplicate row could slip in.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.