Delete all views from Sql Server

假装没事ソ 提交于 2019-12-03 04:48:25

问题


By using this statement in SQL Server:

EXEC sp_msforeachtable 'DROP TABLE ?'

I know it's possible to delete all tables at once.

Is there a similar statement for views? I tried this hoping to be lucky: EXEC sp_msforeachview 'DROP VIEW ?' but it doesn't work!


回答1:


Here you have, no cursor needed:

DECLARE @sql VARCHAR(MAX) = ''
        , @crlf VARCHAR(2) = CHAR(13) + CHAR(10) ;

SELECT @sql = @sql + 'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(v.name) +';' + @crlf
FROM   sys.views v

PRINT @sql;
EXEC(@sql);



回答2:


declare @SQL nvarchar(max)

set @SQL = 
  (
  select 'drop view '+name+'; '
  from sys.views
  for xml path('')
  )

exec (@SQL)



回答3:


All answers don't account for constraints between views. This script will take this into account:

SET @schemeName = 'dbo'

SELECT @name = 
(SELECT TOP 1 o.[name] 
 FROM sysobjects o
 inner join sys.views v ON o.id = v.object_id
 WHERE SCHEMA_NAME(v.schema_id) =@schemeName AND o.[type] = 'V' AND o.category = 0 AND o.[name] NOT IN
 (
    SELECT  referenced_entity_name
    FROM sys.sql_expression_dependencies AS sed
    INNER JOIN sys.objects AS o ON sed.referencing_id = o.object_id
    WHERE referenced_schema_name = @schemeName
 )
 ORDER BY [name])

 WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP VIEW [' + @schemeName + '].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped View: ' + @name
SELECT @name = 
(SELECT TOP 1 o.[name] 
 FROM sysobjects o
 inner join sys.views v ON o.id = v.object_id
 WHERE SCHEMA_NAME(v.schema_id) = @schemeName AND o.[type] = 'V' AND o.category = 0 AND o.[name] NOT IN
 (
    SELECT  referenced_entity_name
    FROM sys.sql_expression_dependencies AS sed
    INNER JOIN sys.objects AS o ON sed.referencing_id = o.object_id
    WHERE referenced_schema_name = @schemeName
 )
 ORDER BY [name])
END
GO

This loops to all views and selects the TOP 1 view that is not present in the references systable.




回答4:


I wanted a script to drop schema bound views in the correct dependency order, and I wanted it to run on sql azure where sys.dm_sql_referencing_entities is not available. I also wanted to be able to view the sql being run before I actually ran it - which you can't do with the script in the answer by @RicNet. So I wrote this recursive query that use the other answers here as a foundation

DECLARE @sql VARCHAR(MAX) = ''
DECLARE @crlf VARCHAR(2) = CHAR(13) + CHAR(10);

;WITH allviews as
( --just combining schema and name
SELECT
    object_id,
    '[' + SCHEMA_NAME(schema_id) + '].[' + name + ']' AS viewname
FROM sys.views
),
dependents AS
( 
SELECT
    referencing.viewname dependentname,
    referenced.viewname dependenton
FROM sys.sql_expression_dependencies r
    INNER JOIN allviews referencing
        ON referencing.object_id = r.referencing_id
    INNER JOIN allviews referenced
        ON referenced.object_id = r.referenced_id
)
,
nodependents 
AS
( 
SELECT
    viewname name
FROM allviews v
    LEFT JOIN dependents d
        ON d.dependentname = viewname
WHERE d.dependentname IS NULL
)
,hierarchy AS
( --the hierarchy recurses the dependencies
SELECT
    d.dependenton,
    d.dependentname,
    1 tier
FROM dependents d UNION ALL SELECT
    d.dependenton,
    d.dependentname,
    h.tier + 1
FROM dependents d
    INNER JOIN hierarchy h
        ON h.dependenton = d.dependentname
--best thing I could think to stop the recursion was to 
--stop when we reached an item with no dependents       
WHERE h.dependenton NOT IN (SELECT
    name
FROM nodependents)
    ),
combined as
( --need to add item with no dependents back in
SELECT
    0 tier,
    name
FROM nodependents UNION SELECT
    tier,
    dependentname
FROM hierarchy  
)
SELECT
    @sql = @sql + 'DROP VIEW ' + name + ';' + @crlf
FROM combined
GROUP BY name --need to group because of multiple dependency paths
ORDER BY MAX(tier) desc

PRINT @sql;

--commented out until I'm confident I want to run it
--EXEC(@sql)



回答5:


Try this script

DECLARE @viewName varchar(500)
DECLARE cur CURSOR
      FOR SELECT [name] FROM sys.objects WHERE type = 'v'
      OPEN cur

      FETCH NEXT FROM cur INTO @viewName
      WHILE @@fetch_status = 0
      BEGIN
            EXEC('DROP VIEW ' + @viewName)
            FETCH NEXT FROM cur INTO @viewName
      END
      CLOSE cur
      DEALLOCATE cur

See here for more info




回答6:


But what about schema?
The below script will help you if the views are part of schema

DECLARE @sql VARCHAR(MAX)='';
SELECT @sql=@sql+'DROP VIEW '+name +';' FROM 
(
SELECT Name=[s].name  + '.' + [v].name FROM sys.views [v]
LEFT OUTER JOIN sys.schemas [s]
ON
(
    [v].[schema_id]=[s].[schema_id]
)
)
X
EXEC(@sql)



回答7:


Since no one of the scripts I tried from the answers worked correctly in the case of multiple schemas I am including a working one.

--DBNAME, PUT YOU OWN ONE use SIPE_ISU

DECLARE @viewName varchar(500) DECLARE cur CURSOR FOR SELECT sk.name + '.'+so.name FROM sys.objects so inner join sys.schemas sk on sk.schema_id = so.schema_id WHERE type = 'v' OPEN cur FETCH NEXT FROM cur INTO @viewName WHILE @@fetch_status = 0 BEGIN EXEC('DROP VIEW ' + @viewName) FETCH NEXT FROM cur INTO @viewName END CLOSE cur DEALLOCATE cur




回答8:


DECLARE @sql VARCHAR(MAX) = ''
        , @crlf VARCHAR(2) = CHAR(13) + CHAR(10) ;

SELECT @sql = @sql + 'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(v.name) +';' + @crlf
FROM   sys.views v

PRINT @sql;
EXEC(@sql);


来源:https://stackoverflow.com/questions/11689557/delete-all-views-from-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!