最近修改了數據庫表結構,數據同步的時候出了問題,發現不少數據明明已經修改,可是經過視圖篩選出來的仍是原來的數據,因此懷疑應該是視圖緩存了數據,在園子裏找到下面的博文,在這裏作個記錄備忘。html
原文連接:http://www.cnblogs.com/yashen/archive/2004/12/23/81000.htmlsql
咱們在使用SqlServer時常常遇到這種狀況,當修改某個表的結構後,相關的視圖就不對了而致使程序錯誤,所以就有個下面這個存儲過程。數據庫
CREATE
PROCEDURE
RefreshAllView
AS
DECLARE
MyCursor
CURSOR
FOR
select
Name
from
dbo.sysobjects
where
OBJECTPROPERTY(id, N
'IsView'
) = 1
and
(
not
name
in
(
'sysconstraints'
,
'syssegments'
))
DECLARE
@
name
varchar
(40)
OPEN
MyCursor
FETCH
NEXT
FROM
MyCursor
INTO
@
name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
begin
exec
sp_refreshview @
name
end
FETCH
NEXT
FROM
MyCursor
INTO
@
name
END
CLOSE
MyCursor
DEALLOCATE
MyCursor
|