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 9203561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:30:01+00:00 2026-06-17T23:30:01+00:00

My table contains these values Name VARCHAR(50), Program VARCHAR(50), Branch VARCHAR(30), TotalP INT, TotalN

  • 0

My table contains these values

Name VARCHAR(50),
Program VARCHAR(50),
 Branch VARCHAR(30),
TotalP INT,
 TotalN INT)

I’ve been trying to INSERT records into this table IF the Name doesn’t already exist. This has probably already been answered on here but when I type something into the search it comes up with 7000+ results and I looked through 100+ and still didn’t find the answer. Any and all help is appreciated.

  • 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-17T23:30:02+00:00Added an answer on June 17, 2026 at 11:30 pm

    If you aren’t worried about concurrency, the following would work:

    IF NOT EXISTS (
        SELECT 1
        FROM YourTable
        WHERE Name = @Name
    )
    BEGIN
        INSERT INTO YourTable (Name, Program, Branch, TotalP, TotalN)
        VALUES (@Name, @Program, @Branch, @TotalP, @TotalN)
    END
    

    The problem here would be that if two processes attempted to add the same name at the same moment… both would pass the IF NOT EXISTS check, and then both would INSERT.

    To prevent this, you can either add a unique constraint to the name column, which would cause one of the inserts to fail, or you could lock the table in your NOT EXISTS check using WITH (UPDLOCK, HOLDLOCK), which would reduce concurrency but not raise any errors.

    EDIT:

    If the values that you want are from a SELECT statement, the following would work:

    INSERT INTO YourDestinationTable (Name, Program, Branch, TotalP, TotalN)
    SELECT st.Name, st.Program, st.Branch, st.TotalP, st.TotalN
    FROM 
        YourSourceTable st
        LEFT JOIN YourDestinationTable dt
            ON dt.Name = st.Name
    WHERE 
        dt.Name IS NULL
        -- Add in any other conditions for YourSourceTable here...
    

    This will insert the row from YourSourceTable if the row is not found in YourDestinationTable… implemented using a LEFT JOIN and IS NULL check.

    This has the same caveats as before… if two processes run this statement simultaneously, you can end up with duplicate name values; either add a unique constraint on name, or perform a (UPDLOCK, HOLDLOCK) on the row in the destination table that you’re testing for existence inside of a transaction:

    BEGIN TRANSACTION
        IF NOT EXISTS (
            SELECT 1
            FROM 
                YourSourceTable st
                JOIN YourDestinationTable dt WITH (UPDLOCK, HOLDLOCK) 
                    ON dt.Name = st.Name
            -- WHERE 
                -- Add in any other conditions for YourSourceTable here...
        )
        BEGIN
            INSERT INTO YourDestinationTable (Name, Program, Branch, TotalP, TotalN)
            SELECT Name, Program, Branch, TotalP, TotalN
            FROM YourSourceTable st
            -- WHERE 
            -- Add in any other conditions for YourSourceTable here...
        END
    COMMIT
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have varchar column Name in table. Rows contain values, for example 'Test', 'tEst',
I have a table like these: Club Table contains: ClubID(IDENTITY), Name, Address Genre table
i have a table named record which contains four column branch,account,name,month. now i want
I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.
Table is named as MasterTable Columns ID type BIGINT , Name type VARCHAR(200) (stores
I have a table which contains a set of templates. These templates have placeholders
I have a table TableX . It contains three columns: A INT , B
a simple table contains - id , name , text . I need to
With this schema: create table object ( obj_id serial primary key, name varchar(80) not
I want to retrieve column values of Name from 3 different tables Table1 contains

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.