SQL Server: copy data from one column to another column?

12,507

To copy the id from anomaly_fee to anamoly_log_update use :

INSERT INTO anamoly_log_update (anamoly_id)
      SELECT anamoly_id FROM anomaly_fee

with both columns it looks like that:

INSERT INTO anamoly_log_update (anamoly_id,PID)
      SELECT anamoly_id,PID FROM anomaly_fee
Share:
12,507
deception1
Author by

deception1

Updated on June 04, 2022

Comments

  • deception1
    deception1 almost 2 years

    I have two tables with the same column anomaly_id. I want to copy the row of anomaly_id from the first table to the second table using this code

    UPDATE amb.anamoly_log_update
    SET anamoly_id = t2.anomaly_id
    FROM amb.anamoly_log_update t1 
    INNER JOIN amb.anomaly_fee t2 ON t1.anamoly_id=t2.anomaly_id
    

    Even after I did that it shows 0 rows affected, when there is data in amb.anomaly.fee (source table)

    Please help

    Edit: Comment from post: I just want to copy all the anamoly_id from amb.anamoly_fee to amb.anamoly_log_update. My code might be nonsensical. Please do review it.

  • deception1
    deception1 over 12 years
    I am new to sql i just want to copy data from a filled column to a empty one on the next table
  • deception1
    deception1 over 12 years
    actually no, i just want to copy all the anamoly_id from amb.anamoly_fee to amb.anamoly_log_update.My code might be nonsensical.Please do review it.
  • rauschen
    rauschen over 12 years
    I understand that but currently you have two filled columns .. please describe the table structure better ... Or maybe an INSERT is what you want
  • deception1
    deception1 over 12 years
    table 1 stu_id PID anamoly_id ||table 2 PID Anomaly_id||Table 1(amb.anamoly.fee is full and Table 2 (amb.anamoly_log_update) is empty.I want to copy all the data from anamoly ID in table 1 to the anamoly_id column in table 2
  • naresh
    naresh over 12 years
    can you please update the question with tables schemas? I don't understand your previous comment
  • naresh
    naresh over 12 years
    INSERT INTO anamoly_log_update (anamoly_id) SELECT anamoly_id FROM anamoly_fee
  • deception1
    deception1 over 12 years
    Thanks Rauschen.It was so simple,and i was wrappinf my head around it bad.Question answered
  • rauschen
    rauschen over 12 years
    You can mark the answer as "accepted" than it will be shown as first answer under you question .. and other see that the problem is solved
  • naresh
    naresh over 12 years
    @user1151462 Please accept the answer, if it served the purpose
  • Andriy M
    Andriy M over 12 years
    @user1151462: This thread will help you to see how, when, why to accept an answer and what answer to accept: How does accepting an answer work?