Home > other >  SQL copy data from a column to another column from another table
SQL copy data from a column to another column from another table

Time:11-01

I have two tables DEP and DEPARTMENTS.

I want to copy the data from DEPARTMENT's column called ID_DEPARTMENT to DEP's column called ID, but I keep getting either errors or says that nothing was updated.

CodePudding user response:

Assuming that there is just that column then you can copy the data and insert it into new rows using:

INSERT INTO dep (id )
SELECT id_department FROM DEPARTMENTS;

Which, for the sample data:

CREATE TABLE departments (id_department, col1, col2, col3) AS
SELECT LEVEL, 'a'||LEVEL, 'b'||LEVEL, 'c'||LEVEL FROM DUAL CONNECT BY LEVEL <= 5;

CREATE TABLE dep(id INT);

Then, after the INSERT:

SELECT * FROM dep;

Outputs:

ID
1
2
3
4
5

db<>fiddle here

  • Related