How to insert into a table new records based on info from another table?

סטנלי גרונן

I am using SQL Server 2012 on Windows7. I currently have 2 tables.

The first table:

DeviceID   DeviceSwVersion   DeviceIPAddr   
1          802               172.26.20.1   
115        800               172.26.18.1   
1234       264               172.26.18.3   
4717       264               172.26.19.2  // <- new   
14157      264               172.26.19.1  // <- new  

The second table:

DeviceIPAddr   Status   TimeStamp (default=getdate())  
172.26.20.1    1        2016-02-09 10:25:01   
172.26.18.1    1        2016-02-09 10:30:12   
172.26.18.3    1        2016-02-09 10:33:08     

What I need is a SQL query to insert into 2nd table new rows corresponding to the new DeviceIP that are now present in the first table. Only the new DeviceIPs that are not already there in the 2nd table.

So, finally the 2nd table should look like this:

DeviceIPAddr   Status   TimeStamp // default = getdate()   
172.26.20.1    1        2016-02-09 10:25:01   
172.26.18.1    1        2016-02-09 10:30:12   
172.26.18.3    1        2016-02-09 10:33:08     
172.26.19.2    0        2016-02-10 09:53:00   
172.26.19.1    0        2016-02-10 09:53:01

Remark: Status column is 0 for new inserted rows and TimeStamp is the current date-time (default value filled automatically by getdate() function).

mmuzahid

You could write a AFTER INSERT TRIGGER something like bellow:

CREATE TRIGGER trigger_insert_table_1
   ON table_1 
   AFTER INSERT AS
BEGIN

   INSERT INTO table_2
   ( DeviceIPAddr,
     Status,
     TimeStamp)
   VALUES
   ( NEW.DeviceIPAddr,
     0,
     getdate() );

END;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select records in on table based on conditions from another table?

From Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

From Dev

drop table and insert records from a new select statement (with the correct records)

From Dev

How to insert rows in another table based on insert in first table

From Dev

Insert records into a table based on value of a record in another table

From Dev

Using Trigger to Create new records using after insert and select from another Table

From Dev

SQL - Insert new row into an existing table based on another table entry

From Dev

How to use MySql 'Insert Ignore into' query to insert records based on some values from a different table

From Dev

insert records into table where one filed is from another table

From Dev

How to insert records from one table to another using trigger in SQL Server?

From Dev

How to insert values into table based off values of another table in SQL?

From Dev

Insert records into one table using key from another table

From Dev

MySQL query to insert record into table based on records in another table

From Dev

Retrieving records from one table based in 'similar' values of another table

From Dev

Select from a table based on another table Number of records Oracle

From Dev

Insert (multiple) new rows into a table from another table using a subquery?

From Dev

How to update the records in a table based on comparing with the id column from another table?

From Dev

Insert records into a table based on value of a record in another table

From Dev

How to insert records in loop in MySQL based on results from another table

From Dev

How to use MySql 'Insert Ignore into' query to insert records based on some values from a different table

From Dev

Insert new records in second table based on colums in first table

From Dev

Insert new rows into table but copy data from another row in the table

From Dev

Insert id's from one table to another based on another column

From Dev

Insert Data From One Table to Another Table and Add New Values

From Dev

How to insert multiple records from one table into another on update of third table in MySQL

From Dev

How to select data from one table and insert it into another table with a new column

From Dev

How to update the records from another table

From Dev

How to fetch records from another table in a query?

From Dev

How to select records from a table when a condition of another table is satisfied

Related Related

  1. 1

    Select records in on table based on conditions from another table?

  2. 2

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  3. 3

    drop table and insert records from a new select statement (with the correct records)

  4. 4

    How to insert rows in another table based on insert in first table

  5. 5

    Insert records into a table based on value of a record in another table

  6. 6

    Using Trigger to Create new records using after insert and select from another Table

  7. 7

    SQL - Insert new row into an existing table based on another table entry

  8. 8

    How to use MySql 'Insert Ignore into' query to insert records based on some values from a different table

  9. 9

    insert records into table where one filed is from another table

  10. 10

    How to insert records from one table to another using trigger in SQL Server?

  11. 11

    How to insert values into table based off values of another table in SQL?

  12. 12

    Insert records into one table using key from another table

  13. 13

    MySQL query to insert record into table based on records in another table

  14. 14

    Retrieving records from one table based in 'similar' values of another table

  15. 15

    Select from a table based on another table Number of records Oracle

  16. 16

    Insert (multiple) new rows into a table from another table using a subquery?

  17. 17

    How to update the records in a table based on comparing with the id column from another table?

  18. 18

    Insert records into a table based on value of a record in another table

  19. 19

    How to insert records in loop in MySQL based on results from another table

  20. 20

    How to use MySql 'Insert Ignore into' query to insert records based on some values from a different table

  21. 21

    Insert new records in second table based on colums in first table

  22. 22

    Insert new rows into table but copy data from another row in the table

  23. 23

    Insert id's from one table to another based on another column

  24. 24

    Insert Data From One Table to Another Table and Add New Values

  25. 25

    How to insert multiple records from one table into another on update of third table in MySQL

  26. 26

    How to select data from one table and insert it into another table with a new column

  27. 27

    How to update the records from another table

  28. 28

    How to fetch records from another table in a query?

  29. 29

    How to select records from a table when a condition of another table is satisfied

HotTag

Archive