if (object_id('dbo.view_name') is not null) exec ('alter view dbo.view_name ...') else exec ('create view dbo.view_name ....')
[其他解释]
对。只能这样做了。
否则只能是删除再创建。 if exists(select * from dbo.sysobjects where id = object_id('View_1') and type='v') drop view view_1 go create view view_1 as select 1 as a go
if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[v_1]') and OBJECTPROPERTY(id, N'IsView') = 1) PRINT '存在' else PRINT '不存在'
如果不存在,看看视图有没有(是否是数据库弄错了) 如果存在,你试试用 begin end 把你整个更新视图的语句包起来 if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[v_1]') and OBJECTPROPERTY(id, N'IsView') = 1) begin 整个更新的视图语句 end [其他解释] 你不需要alter啊,直接删除重建,视图又不包含数据,和存储过程那样,直接删除 [其他解释] 如果是2008 的话,可以使用: SELECT * FROM sys.views WHERE name='xxxx'来查询是否存在视图。 [其他解释] if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[v_1]') and OBJECTPROPERTY(id, N'IsView') = 1) GO------ alter view v_1
if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[View_1]') and OBJECTPROPERTY(id, N'IsView') = 1) begin alter VIEW [dbo].[View_1] AS SELECT InstanceID, ServiceContract FROM dbo.ActiveConfigHosts
if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[View_1]') and OBJECTPROPERTY(id, N'IsView') = 1) begin alter VIEW [dbo].[View_1] AS SELECT InstanceID, ServiceContract FROM dbo.ActiveConfigHosts
end [其他解释] if exists (select 1 from sys.views where name='View_1') begin alter VIEW [dbo].[View_1] AS SELECT InstanceID, ServiceContract FROM dbo.ActiveConfigHosts