C# Question

SQL Server Insert New Record if Not Exists, If Exists Update In C#

I Need to know SQl Query, how to insert new record into sql server if the record is not exists or if the record exists update the record.

The below one what i tried

string sql= "if record not exists (select * from OpCircular Where circular_No='" + tbcicularno.Text.Trim() + "') INSERT INTO OpCircular(circular_No, fromDate, toDate, empId) VALUES ('" + tbcicularno.Text.Trim() + "','" +from+ "','" + to + "','Testing') If not Exists Update Set fromDate='" +from+ "'"

Answer Source

you can do like this, example: if exists(select 1 from table where id = 4) begin (do what u want) end else begin (do what u want) end

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download