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, 2022 in Database by Kithuzzz
• 38,000 points
672 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, 2022 by narikkadan
• 63,600 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, 2022 in Database by Kithuzzz
• 38,000 points
688 views
0 votes
0 answers

How to update Identity Column in SQL Server?

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

Aug 9, 2022 in Database by Kithuzzz
• 38,000 points
1,359 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, 2022 in Database by Kithuzzz
• 38,000 points
1,615 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, 2022 in Database by Kithuzzz
• 38,000 points
1,234 views
0 votes
1 answer

SQL Server CASE .. WHEN .. IN statement

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

answered Feb 8, 2022 in Database by Vaani
• 7,070 points
686 views
0 votes
0 answers

Selecting COUNT(*) with DISTINCT

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

Feb 14, 2022 in Database by Neha
• 9,020 points
490 views
0 votes
1 answer

SQL Server CASE .. WHEN .. IN statement

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

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

Querying for the second largest salary

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

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

How to execute function in SQL Server 2008

The function appears not to be being ...READ MORE

answered Sep 11, 2022 in Database by narikkadan
• 63,600 points
955 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, 2022 in Database by narikkadan
• 63,600 points
914 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