Wednesday 27 March 2013

SQL Query: IF ELSE Statement - Insert if not exist otherwise Update

Scenario:
I want to INSERT if NOT Exist otherwise I want to UPDATE recrod

Solution:
Example1:
IF EXISTS (SELECT NULL FROM MyTable WHERE ScorerID = @Blah AND CONVERT(VARCHAR, DateCreated, 101) = CONVERT(VARCHAR, GETDATE(), 101))
    UPDATE MyTable SET blah blah blah
ELSE
    INSERT INTO MyTable blah blah blah

Example2:
INSERT INTO AggregatedData (datenum,Timestamp)
VALUES ("734152.979166667","2010-01-14 23:30:00.000")
ON DUPLICATE KEY UPDATE
  Timestamp=VALUES(Timestamp)

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home