Quería que un script descartara las vistas enlazadas al esquema en el orden de dependencia correcto, y quería que se ejecutara en sql azure donde sys.dm_sql_referencing_entities
no está disponible. También quería poder ver el sql que se ejecuta antes de que realmente lo ejecutara, lo cual no se puede hacer con el script en la respuesta de @RicNet. Así que escribí esta consulta recursiva que utilizan las otras respuestas aquí como una base
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)
Que su tratado 'DROP VIEW?' EXEC sp_msforeachTABLE ? –