snp.it
snp.it

Reputation: 522

Take one table values and insert it into another Table

I want to insert the table 2 values into table 1.

My tables

Table 1

       Month   Gender    State  Load     DepartmentID DeptName  Count1 Count2 Count3
         Jan   Male       va    FullTime   100         HR         2      0      1
         Jan   Male       VA    PartTime   100         HR         1      3     13
         Jan   Female     Va    FullTime   100         HR         2      1     21
         Jan   Female     VA    PartTime   100         HR         0      0     0

Table 2

       Month   Gender    State  Load     DepartmentID DeptName  Count1 Count2 Count3
         Jan   Male       va    FullTime   220         Mhrm         2      0      1
         Jan   Male       VA    PartTime   220        Mhrm         1      3     13
         Jan   Female     Va    FullTime   220        Mhrm         2      1     21
         Jan   Female     VA    PartTime   220         Mhrm         0      0     0

The table1 has a lot more values.... But I am trying to add the Department ID 220 and Dept Name Mhrm to Table 1. How can I achieve this?

EDIT:

The above tables are not direct tables, but couple of views and a lot of conditions combined. I wanted to know how I can combine then dynamically without changing any data on the tables.

Upvotes: 0

Views: 260

Answers (2)

Gaston Flores
Gaston Flores

Reputation: 2467

Assuming that DepartamentID is not a identity field, try this query:

    INSERT INTO TABLE2(FIELD1,...,FIELDN)
    SELECT FIELD1,...,FIELDN FROM TABLE1.

Otherwise, you should run this before:

    set identity_insert TABLE2 ON
    --INSERT QUERY
    set identity_insert TABLE2 OFF

Upvotes: 3

Franck Dernoncourt
Franck Dernoncourt

Reputation: 83157

INSERT INTO table1
SELECT * FROM table2;

Upvotes: 1

Related Questions