Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8090179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:38:24+00:00 2026-06-05T19:38:24+00:00

I have table in which I am inserting rows for employee but next time

  • 0

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

How can we do this in SQL Server 2005?

I am using jsp

my query is

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

how to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:38:28+00:00Added an answer on June 5, 2026 at 7:38 pm

    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
    

    You could easily wrap this into a stored procedure and just call that stored procedure from the outside (e.g. from a programming language like C# or whatever you’re using).

    Update: either you can just write this entire statement in one long string (doable – but not really very useful) – or you can wrap it into a stored procedure:

    CREATE PROCEDURE dbo.InsertOrUpdateEmployee
           @ID INT,
           @Name VARCHAR(50),
           @ItemName VARCHAR(50),  
           @ItemCatName VARCHAR(50),
           @ItemQty DECIMAL(15,2)
    AS BEGIN
        IF NOT EXISTS (SELECT * FROM dbo.Table1 WHERE ID = @ID)
           INSERT INTO dbo.Table1(ID, Name, ItemName, ItemCatName, ItemQty)
           VALUES(@ID, @Name, @ItemName, @ItemCatName, @ItemQty)
        ELSE
           UPDATE dbo.Table1
           SET Name = @Name,
               ItemName = @ItemName,
               ItemCatName = @ItemCatName,
               ItemQty = @ItemQty
           WHERE ID = @ID
    END
    

    and then just call that stored procedure from your ADO.NET code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a procedure in which i am inserting record in employee table.nad getting
I have a table A into which I am inserting data. Then some calculation
I have a table which lists all events on a debating calendar. Each row
I have a table which extends AbstractTableModel. When data for it arrives, i delete
I have db table which has location details like postal code, long/lat data, region,
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
I have two data tables which have a primary key column in common but
I have a table in SQL Server 2005 which contains some changelog data for
I currently have a table in MySQL that stores values normally, but I want
We have a loop in our PHP code which inserts rows into a table.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.