视图刷新我有很多视图..大概是这样创建的!!
create view kjkdf
as
select a.*,b.col2 from tablename1 a left join tablename2 b 
on a.col1=b.col1 但我现在在tablename1 里加了个字段后,视图的字段就都乱了!
这是为什么呢?
有什么办法解决吗??我有很多哦,包括视图调用视图哦!!
谢谢

解决方案 »

  1.   

    create PROC [dbo].[sp_recompile_view]AS
    BEGIN
    declare @sql nvarchar(3000)
    declare @viewname varchar(250)
    declare @objName varchar(250)
    declare @i int declare #_cursor cursor for 
    select name  from sysobjects where type='U' order by type
    open #_cursor fetch next from #_cursor into @objName
    while @@fetch_status=0
     begin   
    set @sql=N'ALTER INDEX ALL ON '+@objName+
    ' REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON,'+
    '   STATISTICS_NORECOMPUTE = ON) '
           PRINT @sql                    
        EXECUTE sp_executesql @sql,N'@objName varchar(250)', @objName=@objName     fetch next from #_cursor into @objName
     end
    close #_cursor
    deallocate #_cursor
    declare #_cursor cursor for 
    select name  from sysobjects where type='TR' OR type='P'  order by type
    open #_cursor fetch next from #_cursor into @objName
    while @@fetch_status=0
     begin   
      exec sp_recompile @objName  
      fetch next from #_cursor into @objName
     end
    close #_cursor
    deallocate #_cursor
    set @i=0
    declare #_cursor cursor for 
    select name  from sysobjects where type='V' open #_cursor
    fetch next from #_cursor into @viewname while @@fetch_status=0
     begin
      print '成功刷新视图: '+ @viewname 
      exec sp_refreshview @viewname  
      set @i= @i +1 
      fetch next from #_cursor into @viewname
     end close #_cursor
    deallocate #_cursor
    print '完成'
    print '共成功刷新' + convert(varchar(10),@i) + '个视图'