Sunday, March 18, 2012

sp_DropView: A simple T-SQL stored proc to drop a view in any database


A simple T-SQL procedure to drop a view in any database of the current server. The goal is to have the view removed, was it there or not. Feel free to use...




CREATE PROCEDURE [dbo].[sp_DropView]
@Database nvarchar(255),
  @Owner nvarchar(255),
@View nvarchar(255)
AS
BEGIN
  DECLARE @SQLStmt varchar(8000);
SET NOCOUNT ON;
SET @SQLStmt = '
IF  EXISTS (
  SELECT * FROM ' + @Database + '.sys.views
  WHERE
  object_id = OBJECT_ID(N''' + @Database + '.' + @Owner + '.' + @View + ''')
  )
  EXEC ' + @Database + '..sp_executesql N''DROP VIEW ' + @Owner + '.' + @View + '''
  ';
  EXEC (@SQLStmt);
END
GO



Eric

No comments:

Post a Comment