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

  • Home
  • SEARCH
  • 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 7733347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:57:53+00:00 2026-06-01T06:57:53+00:00

I’ve unique rows with primary key in my emp table. select * from EMP

  • 0

I’ve unique rows with primary key in my emp table.

select * from EMP E;

Now I’ve created emp_backup table which takes backup of emp table like.

insert into emp_backup
select * from emp e
where not exists (select EMPNO from emp_backup E1 where E1.EMPNO=e.EMPNO);

the above query successfully copies all the rows from emp to emp_backup and it make sure when you again run above query it will not copy the existing rows from emp to emp_backup table in next run.

now my problem is when i update any record in emp table and try to run above query it gives me error of primary key violation which is excepted.

for example.

update  EMP
set JOB='worker'
where   EMPNO=14;

I update emp table after copying the record with empno 14 to emp_backup table.

when i run insert into emp_backup.... query i want this update change should refelect into emp_backup table.

How can i modify the above query so that it will copy the updated rows with existing primary key from emp to emp_backup.

I hope my question is clear, tell me how i can improve it.

  • 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-01T06:57:55+00:00Added an answer on June 1, 2026 at 6:57 am

    It seems that when you created your emp_backup table, that you also created a table PK on the EMPNO table. This will prevent you from inserting the same emp record twice.

    You need to rethink your ‘backup’ plan – do you want

    1. a running audit of every version of every record change in the emp table
    2. a mirror of the emp table exactly?

    For 1:

    • Drop the primary key on emp_backup table. This will allow duplicate emp records to be copied into it. If you need idempotence on the emp_backup table, you could create a new surrogate PK (e.g. emp_backup_id int identity(1,1), and possibly a new CURRENT_TIMESTAMP defaulted column to assist you in determining which is the latest

    After dropping your PK, updated records can be ‘backed up’ as:

    insert into emp_backup 
    select * from emp e 
    where exists 
      (select EMPNO 
       from emp_backup E1 
       where E1.EMPNO=e.EMPNO
       and 
       (e1.othercolumn1 <> e.othercolumn1
       or e1.othercolumn2 <> e.othercolumn2
       ...
       )
    ); . 
    

    (Note that columns of certain types can’t be compared directly)

    For 2, you would need to update existing records in your emp_bak table:

    update eb
    set eb.othercolumn1 = e.othercolumn1,
        eb.othercolumn2 = e.othercolumn2
    from emp_backup eb
        inner join emp e on eb.empno = e.empno
    where 
       eb.othercolumn1 <> e.othercolumn1
       or eb.othercolumn2 <> e.othercolumn2
       ...
    ); . 
    

    But your table mirroring begs the question, everytime you want a backup, why not just drop your existing emp_backup table and do:

    insert into emp_backup 
    select * from emp e 
    

    ??

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.