|
The following
Microsoft SQL Server 2008 T-SQL scripts demonstrate the use of EXISTS in conditional expressions.
-- T-SQL check table existence before dropping view or table
-- Create test table with SELECT INTO
USE AdventureWorks2008;
SELECT * INTO Product1 FROM AdventureWorks2008.Production.Product
GO
-- (504 row(s) affected)
CREATE VIEW vProduct1 AS SELECT * FROM Product1
GO
IF EXISTS (SELECT * FROM sys.views
WHERE object_id = OBJECT_ID(N'[dbo].[vProduct1]'))
DROP VIEW [dbo].[vProduct1]
GO
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Product1]')
AND type in (N'U'))
DROP TABLE [dbo].[Product1] GO
-- DROP stored procedure if exists
IF EXISTS ( SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[uspCustomerListByState]')
AND TYPE IN (N'P',N'PC'))
DROP PROCEDURE [dbo].[uspCustomerListByState]
GO |