将行插入到视图使用的表中时更新SQL视图

时间:2014-12-03 19:32:13

标签: sql-server triggers sql-view

我正在研究从View和SQL表LastTrade检索数据的SQL View,我使用网页将数据插入到表LastTrade中,当我插入一行时,我想要我的SQL视图已更新。

我的观点如下

CREATE VIEW [dbo].[CanadianCrudes]
AS
  SELECT a.Id,
         a.Product,
         a.Grade,
         a.Term,
         a.Pipeline,
         a.Location,
         a.[Index],
         a.bidvolume,
         a.Bidcp,
         a.Bid,
         a.Offer,
         a.offercp,
         a.offervolume,
         a.TermID,
         a.ProductID,
         a.GradeID,
         a.Locked,
         a.Hold,
         a.offercpid,
         a.bidcpid,
         a.idbid,
         a.[sequence],
         CASE
           WHEN ROW_NUMBER()
                  OVER (
                    PARTITION BY a.ProductID, a.GradeID, a.TermID, a.Pipeline, a.[Index]
                    ORDER BY a.Bid DESC, a.Offer) = 1 THEN b.LastTradeValue
           ELSE NULL
         END AS LastTradeValue
  FROM   CanadianCrudesRaw AS a
         LEFT JOIN (SELECT Product,
                           Grade,
                           Term,
                           Pipeline,
                           [Index],
                           MIN(LastTradeValue) LastTradeValue
                    FROM   LastTrades
                    GROUP  BY Product,
                              Grade,
                              Term,
                              Pipeline,
                              [Index]) AS b
                ON b.Product = a.Product
                   AND b.Grade = a.Grade
                   AND b.Term = a.Term
                   AND ISNULL(b.Pipeline, '00') = ISNULL(a.Pipeline, '00')
                   AND ISNULL(b.[Index], '00') = ISNULL(a.[Index], '00')

如何实现刷新视图以获取插入数据的触发器?

1 个答案:

答案 0 :(得分:0)

只需执行以下查询

SELECT * FROM [dbo].[CanadianCrudes]