删除从SQL Server的所有意见(Delete all views from Sql Serve

2019-07-30 16:14发布

通过使用SQL Server这样的说法:

EXEC sp_msforeachtable 'DROP TABLE ?'

我知道这是可以一次删除所有表。

是否有意见了类似的声明? 我想这希望是幸运的:“?DROP VIEW” EXEC sp_msforeachview 但它不工作!

Answer 1:

在这里,你有没有需要光标:

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);


Answer 2:

declare @SQL nvarchar(max)

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

exec (@SQL)


Answer 3:

所有答案不占视图之间的约束。 这个脚本会考虑到这一点:

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

这个循环到所有视图和选择TOP 1认为是不存在于参考SYSTABLE。



Answer 4:

我想一个脚本,删除绑定到架构的观点在正确的依赖顺序,我希望它在SQL Azure上运行,其中sys.dm_sql_referencing_entities不可用。 我也希望能够看到之前,我居然跑它正在运行的SQL - 你不能在由@RicNet答案脚本做。 所以我写了,这里使用其他的答案为基础这个递归查询

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)


Answer 5:

试试这个脚本

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

在这里看到更多信息



Answer 6:

但有关架构是什么?
下面的脚本将帮助你,如果意见是模式的一部分

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)


Answer 7:

由于没有从我的答案试图脚本的一个多模式的情况下工作正常,我包括一个工作之一。

--DBNAME, PUT YOU OWN ONE use SIPE_ISU

DECLARE @viewName VARCHAR(500)DECLARE CUR CURSOR FOR SELECT sk.name + '' + so.name FROM sys.objects中以便内部联接sys.schemas SK上sk.schema_id = so.schema_id WHERE类型= '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



Answer 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);


文章来源: Delete all views from Sql Server