如何根据另一个表中的信息将新记录插入表中?

时间:2016-02-10 09:37:51

标签: sql sql-server insert record

我在Windows7上使用SQL Server 2012。我目前有两张桌子。

第一张表:

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  

第二张表:

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     

我需要的是一个SQL查询,用于在第二个表中插入与第一个表中现在存在的新DeviceIP对应的新行。 只有第二个表中不存在的新 DeviceIP

所以,最后第二个表应该是这样的:

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

备注:Status列对于新插入行为0,TimeStamp为当前日期时间(默认值由getdate()函数自动填充)。

7 个答案:

答案 0 :(得分:2)

合并是另一种方式..

 MERGE <[The second table]> t
USING [The first table: ] s
  ON t.DeviceIPAddr    = s.DeviceIPAddr   
WHEN NOT MATCHED 
  INSERT (DeviceIPAddr, Status, Timestamp)
  VALUES (s.DeviceIPAddr,0, getutcdate())
;

答案 1 :(得分:1)

insert into secondtable
select deiviceipaddr,0,getdate()
from isttable t1
where not exists(select 1 from secondtable t2 where t1.ipaddress=t2.ipaddress) 

---还有一种方法

insert into secondtable
select deiviceipaddr,status from firsttable
except
select deiviceipaddr,status from secondtable

答案 2 :(得分:1)

 INSERT Table2
 SELECT   DeviceIPAddr, 0, getDate()
 FROM    Table1
 WHERE   DeviceIpAddr NOT IN (SELECT DeviceIpAddr FROM Table2)

答案 3 :(得分:1)

Insert into table2
(DeviceIPAddr, Status, TimeStamp)

SELECT t1.deiviceipaddr, 0, GETDATE()
FROM Table1
Left Outer Join Table2
ON (t1.DeviceIPAddr = t2. DeviceIPAddr)
WHERE t2. DeviceIPAddr IS NULL

答案 4 :(得分:1)

所以你可以使用NOT EXISTS(),就像这样:

INSERT INTO SecondTable 
(SELECT t.DeviceIpAddr , 0 as status,getdate()
 FROM FirstTable t
 WHERE NOT EXISTS(select 1 from SecondTable s where t.DeviceIpAddr  = s.DeviceIpAddr )

或者,您可以像这样使用NOT IN():

 INSERT INTO SecondTable 
(SELECT t.DeviceIpAddr , 0 as status,getdate()
 FROM FirstTable t
 WHERE t.DeviceIpAddr  NOT IN(select distinct s.DeviceipAddr from SecondTable)

答案 5 :(得分:1)

DECLARE @Table1 TABLE 
    (DeviceID int, DeviceSwVersion int, DeviceIPAddr varchar(11))
;

INSERT INTO @Table1
    (DeviceID, DeviceSwVersion, DeviceIPAddr)
VALUES
    (1, 802, '172.26.20.1'),
    (115, 800, '172.26.18.1'),
    (1234, 264, '172.26.18.3'),
    (4717, 264, '172.26.19.2'),
    (14157, 264, '172.26.19.1')
; 

DECLARE @Table2 TABLE 
    (DeviceIPAddr varchar(11), Status int, TimeStamp varchar(19))
;

INSERT INTO @Table2
    (DeviceIPAddr, Status, TimeStamp)
VALUES
    ('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')

使用NOT EXISTS

Select TT.DeviceIPAddr,0 Status,getdate() TimeStamp from @Table1 TT 
        where  NOT EXISTS 
                (select 1 from @Table2 T where T.DeviceIPAddr = TT.DeviceIPAddr)

OR

使用除外

Select TT.DeviceIPAddr,0 AS Status, getdate() AS  TimeStamp from @Table1 TT
EXCEPT 
select T.DeviceIPAddr,0 AS Status, getdate() AS  TimeStamp from @Table2 T

答案 6 :(得分:0)

你可以写一个后插入触发器类似下面的东西:

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;