【问题标题】:Firing an Insert trigger only when there is no data in the target table similar to the data from the inserted仅当目标表中没有与插入的数据相似的数据时才触发插入触发器
【发布时间】:2015-05-05 17:18:56
【问题描述】:

我正在尝试执行一个 SQL 插入触发器,当数据被插入到一个直接触发器的表中时,该触发器被触发并将一些数据插入到另一个表中,但我试图在触发触发器之前检查一个条件,该触发器是查找与插入数据相似的数据是否已经在目标表中。我尝试了以下查询

CREATE trigger [dbo].[trgI_InsertINTOLastTrade]
ON [dbo].[tblCC]
AFTER INSERT 
AS
BEGIN
    SET NOCOUNT ON

   IF(SELECT COUNT(id) AS IDC 
      FROM LastTrades 
      WHERE product = inserted.Product 
        AND grade = inserted.grade 
        AND Term = inserted.Term 
        AND ISNULL(Pipeline, '') = inserted.Pipeline 
        AND ISNULL([Index], '') = inserted.[Index]) != 0
   BEGIN
       RETURN
   END

   INSERT INTO LastTrades(Product, Grade, Term, Pipeline,[Index], LastTradeValue)
       SELECT 
           Product, Grade, Term, Pipeline, [Index], LastTradeValue 
       FROM
           inserted 
END

当我执行上述查询时,我收到以下错误:

消息 4104,级别 16,状态 1,过程 trgI_InsertINTOLastTrade,第 15 行
无法绑定多部分标识符“inserted.Product”。

消息 4104,级别 16,状态 1,过程 trgI_InsertINTOLastTrade,第 15 行
无法绑定多部分标识符“inserted.grade”。

消息 4104,级别 16,状态 1,过程 trgI_InsertINTOLastTrade,第 15 行
无法绑定多部分标识符“inserted.Term”。

消息 4104,级别 16,状态 1,过程 trgI_InsertINTOLastTrade,第 15 行
无法绑定多部分标识符“inserted.Pipeline”。

消息 4104,级别 16,状态 1,过程 trgI_InsertINTOLastTrade,第 15 行
无法绑定多部分标识符“inserted.Index”。

有什么好的方法可以解决这个问题吗?

【问题讨论】:

    标签: sql-server triggers


    【解决方案1】:

    在调用insert.Product之前,需要在“from”语句中声明。

    CREATE trigger [dbo].[trgI_InsertINTOLastTrade]
    on [dbo].[tblCC]
    after insert
    as
    begin
    SET NOCOUNT ON
    
    IF(
        select  count(id) AS IDC 
        from    LastTrades 
                    inner join inserted i
                    on  product=i.Product 
                        and grade=i.grade 
                        and Term=i.Term 
                        and ISNULL(Pipeline,'')=i.Pipeline 
                        and ISNULL([Index],'')=i.[Index]) !=0
    Begin
    Return
    End
    insert into LastTrades(Product,Grade,Term,Pipeline,[Index],LastTradeValue)
    Select Product,Grade,Term,Pipeline,[Index],LastTradeValue from inserted 
    
    end
    

    【讨论】:

      【解决方案2】:

      我假设您只想插入其他表中尚不存在的行。即使第二个表中已经存在给定的行,您对其进行编码的方式也会从您的插入中插入所有行。这是因为 sql 服务器触发器每次操作触发一次。您需要编写触发器来处理此问题。这样的事情应该很接近了。

      CREATE trigger [dbo].[trgI_InsertINTOLastTrade] on [dbo].[tblCC] after insert as 
      begin
      
          SET NOCOUNT ON;
      
          insert into LastTrades(Product,Grade,Term,Pipeline,[Index],LastTradeValue)
          Select i.Product
              , i.Grade
              , i.Term
              , i.Pipeline
              , i.[Index]
              , i.LastTradeValue 
          from inserted i
          left join LastTrades lt on lt.product = inserted.Product 
                  and lt.grade = inserted.grade 
                  and lt.Term = inserted.Term 
                  and ISNULL(lt.Pipeline, '') = inserted.Pipeline 
                  and ISNULL([Index], '') = inserted.[Index]
          where lt.Product is not null
      
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-16
        • 1970-01-01
        • 2015-08-10
        • 1970-01-01
        • 2011-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多