How to upsert update or insert in SQL Server 2005

0 votes

I have a table in which I am inserting rows for employees, but the next time I want to enter a row, I only want to update with the necessary columns if it exits there; if not, I want to construct a new row.

How can we do this in SQL Server 2005?

My Query:

String sql="insert into table1(id,name,itemname,itemcatName,itemQty)values('val1','val2','val3','val4','val5')";

If it's first time then insert it into database else if exists update it. Can someone please help me with this?

Sep 17 in Database by Kithuzzz
• 12,240 points
22 views

1 answer to this question.

0 votes

Try to check for existence:

IF NOT EXISTS (SELECT * FROM dbo.Employee WHERE ID = @SomeID)

    INSERT INTO dbo.Employee(Col1, ..., ColN)
    VALUES(Val1, .., ValN)

ELSE

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

This is something that could be easily turned into a stored procedure and then called from the outside (for example, from a programming language like C# or whatever you're using).

answered Sep 18 by narikkadan
• 20,880 points

Related Questions In Database

0 votes
0 answers

How to update two tables in one statement in SQL Server 2005?

In one operation, I want to update ...READ MORE

Aug 20 in Database by Kithuzzz
• 12,240 points
28 views
0 votes
0 answers

How to update Identity Column in SQL Server?

With 200 records currently, I want to ...READ MORE

Aug 9 in Database by Kithuzzz
• 12,240 points
176 views
0 votes
0 answers

How to find sum of multiple columns in a table in SQL Server 2005?

I have a table Emp which has these rows: Emp_cd ...READ MORE

Aug 19 in Database by Kithuzzz
• 12,240 points
101 views
0 votes
0 answers

I want to use CASE statement to update some records in sql server 2005

UPDATE dbo.TestStudents SET LASTNAME = ( ...READ MORE

Sep 2 in Database by Kithuzzz
• 12,240 points
35 views
0 votes
1 answer

SQL Server CASE .. WHEN .. IN statement

Two forms of CASE statements are getting ...READ MORE

answered Feb 8 in Database by Vaani
• 7,020 points
78 views
0 votes
0 answers

Selecting COUNT(*) with DISTINCT

In SQL Server 2005, I have a ...READ MORE

Feb 14 in Database by Neha
• 8,920 points
108 views
0 votes
1 answer

SQL Server CASE .. WHEN .. IN statement

Try this... SELECT AlarmEventTransactionTableTable.TxnID, ...READ MORE

answered Jun 14 in Others by polo
• 1,480 points
35 views
0 votes
1 answer

Querying for the second largest salary

general SQL query should be as follows: select ...READ MORE

answered Jun 16 in Others by polo
• 1,480 points
27 views
0 votes
1 answer

How to extract only the year from the date in sql server 2008?

To obtain the year from the given ...READ MORE

answered Sep 15 in Database by narikkadan
• 20,880 points
46 views
0 votes
1 answer

How to Execute SQL Server Stored Procedure in SQL Developer?

You don't need EXEC clause. Simply use: proc_name ...READ MORE

answered Sep 16 in Database by narikkadan
• 20,880 points
41 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP