【问题标题】:How to insert into a table new records based on info from another table?如何根据来自另一个表的信息向表中插入新记录?
【发布时间】:2016-02-10 09:37:50
【问题描述】:

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

第一张桌子:

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只有新的DeviceIPs,还没有在第二个表中。

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

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() 函数自动填充)。

【问题讨论】:

  • 尝试为此使用触发器
  • 我会尝试,但我不知道如何。我是 SQL 的初学者。 :( ...请告知。
  • @groenhen ,正如 Paresh 已经告诉我的那样,我认为 AFTER INSERT 触发器是更好的解决方案。由于您是初学者,我的回答让您了解所需的触发器。

标签: sql sql-server insert record


【解决方案1】:

合并是另一种方式..

 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())
;

【讨论】:

    【解决方案2】:
     INSERT Table2
     SELECT   DeviceIPAddr, 0, getDate()
     FROM    Table1
     WHERE   DeviceIpAddr NOT IN (SELECT DeviceIpAddr FROM Table2)
    

    【讨论】:

      【解决方案3】:
      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】:

        所以你可以使用 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】:
          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')
          

          ; 使用不存在

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

          使用例外

          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
          

          【讨论】:

          • 但是就我们根据 DeviceIPaddress @TheGameiswar 过滤记录而言,所以提到日期是默认的
          【解决方案6】:
          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
          

          【讨论】:

            【解决方案7】:

            你可以写一个 AFTER INSERT TRIGGER 类似下面这样:

            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;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-07-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-09
              • 2017-08-01
              相关资源
              最近更新 更多