通过在 SQL Server 中使用此语句:
EXEC sp_msforeachtable 'DROP TABLE ?'
我知道可以一次删除所有表。
是否有类似的观点声明?我试过这个希望幸运:EXEC sp_msforeachview 'DROP VIEW ?' 但它不起作用!
通过在 SQL Server 中使用此语句:
EXEC sp_msforeachtable 'DROP TABLE ?'
我知道可以一次删除所有表。
是否有类似的观点声明?我试过这个希望幸运:EXEC sp_msforeachview 'DROP VIEW ?' 但它不起作用!
在这里,不需要光标:
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);
declare @SQL nvarchar(max)
set @SQL =
(
select 'drop view '+name+'; '
from sys.views
for xml path('')
)
exec (@SQL)
我想要一个脚本以正确的依赖顺序删除模式绑定视图,并且我希望它在sys.dm_sql_referencing_entities
不可用的 sql azure 上运行。我还希望能够在我实际运行它之前查看正在运行的 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)
所有答案都没有考虑视图之间的约束。该脚本将考虑到这一点:
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
这将循环到所有视图并选择引用 systable 中不存在的 TOP 1 视图。
但是模式呢?
如果视图是架构的一部分,以下脚本将为您提供帮助
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)
试试这个脚本
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
由于我从答案中尝试的任何一个脚本都没有在多个模式的情况下正常工作,因此我包括一个工作模式。
--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
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);