Friday, 8 March 2013

Sql server Procedure for if Record insert first time then insert else update the table.....

I have table in which I am inserting rows for employee but next time when I want to insert row I don't want to insert again data for that employee just want to update with required columns if it exits there if not then create new row....


Syntax For Procedure:


CREATE PROCEDURE dbo.InsertOrUdpateEmployee
       @ID INT,
       
AS BEGIN
IF NOT EXISTS (SELECT * FROM Employee WHERE ID = @ID)
    INSERT INTO Employee(Col1, ..., ColN)
    VALUES(Val1, .., ValN)

ELSE

    UPDATE Employee
    SET Col1 = Val1, Col2 = Val2, ...., ColN = ValN
    WHERE ID = @ID
END

No comments:

Post a Comment