1、我做了一个视图[V_price_type_w]。代码为:
SELECT     TOP (100) PERCENT a.id, a.oid, a.leib, a.pinm, a.guig, a.caiz, a.chanc, a.guapj, a.decldate, a.uid, a.reports, a.freshdate, a.yesterday, a.week, a.month, a.anchor, 
                      b.Id AS cityid, a.source
FROM         dbo.price_type AS a CROSS JOIN
                      dbo.C_List AS b
ORDER BY cityid, a.id
  这就是说:表【price_type】是价格框架,表【C_List】是城市列表。以这两个基表生成了视图,实际上只是300个城市、48个需要价格监测的品种的大框架,  2、现在是要从其他表计算后,通过update的办法,修改[guapj]挂牌价的具体数值,也就是说:具体每个城市、每个品种(48X300个)的价格
  3、我已经写了一个其他表的计算后,修改这个视图的触发器(专门计算出具体一个城市、某个品种的guapj,现在需要 update 这个视图中的 guapj)。代码为:
update V_price_type_w  set guapj=@avgprice,decldate=getdate(),uid=@uid where id=@todaypriceid and cityid=@cid  4、事实上,视图里面的数据是不能修改的。这样,又逼迫我在视图中建一个“instead of代替触发器”,来更新视图上游的基表【price_type】和【C_List】。代码如下:
ALTER TRIGGER [dbo].[VpricetypeTrigger_wjf] 
   ON  [dbo].[v_price_type_wjf]
   
   instead of update
   
AS 
BEGIN

SET NOCOUNT ON;
declare @todaypriceid int
    declare @cid int
declare @guapj float
    
    select @guapj=guapj,@cid=cid from updated
begin
    update [price_type]  set a.id=@todaypriceid 
    update [C_List] set b.id = @cid
end

end   请修改一下我的“instead of代替触发器”:
  1、这个代码,能不能实现我的目的?
  1、两个id重复,如何避免?
  2、第二个update 是不是不执行啊?
  

解决方案 »

  1.   

    INSTEAD OF 
    前几天才看过,我又错了
      

  2.   

    [code=SQL]ALTER TRIGGER [dbo].[VpricetypeTrigger_wjf] 
       ON  [dbo].[v_price_type_wjf]
       
       instead of update
       
    AS 
    BEGIN
        
        SET NOCOUNT ON;
     
        begin
            update [price_type]  
                   set guapj=b.guapj,
                   decldate=getdate(),
                   uid=b.uid
           from price_type a,inserted b
           where a.id=b.id
               
            
        end
        
    end [/code]
      

  3.   

    “instead of代替触发器”,来更新视图上游的基表【price_type】和【C_List】,就可以更新视图了
      

  4.   

      1、这个代码,能不能实现我的目的?>>>yes  1、两个id重复,如何避免?>>>只更新price_type的guapj,不更新city
      2、第二个update 是不是不执行啊?
    >>>不用执行
      

  5.   

    ALTER TRIGGER [dbo].[VpricetypeTrigger_wjf] 
       ON  [dbo].[v_price_type_wjf]
       
       instead of update
       
    AS 
    BEGIN
        
        SET NOCOUNT ON;  
        begin
            update p
    set p.guapj=i.guapj
    from price_type p,inserted i 
    where p.id = i.id
        end
        
    end 
      

  6.   

    大师们的代码,在visual studio 下面“单步执行”测试时,
    则跳出“没有可用于当前位置的源代码”字样。恳请大师,再分析一下原因