Find SQL View in All Microsoft Dynamics GP Databases

Microsoft Dynamics GPI recently needed to find which databases in SQL Server had a specific view deployed to them. I’ve created scripts in the past to find SQL objects in all databases (table,trigger and functions), but not one for SQL views.

Some of the previous scripts looked in all databases and others were limited to only the Microsoft Dynamics GP databases; this script is one of the latter, using the SY015000 table to only search for a SQL view in the Dynamics GP databases:

/*
Created by Ian Grieve of azurecurve | Ramblings of an IT Professional (http://www.azurecurve.co.uk) This code is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0 Int). */
DECLARE @command NVARCHAR(MAX) DECLARE @SystemDatabase VARCHAR(15) = 'D20' DECLARE @View VARCHAR(50) = 'uv_AZRCRV' CREATE TABLE #ReturnedData( DBNAME VARCHAR(15) ,VIEWNAME VARCHAR(100) ) SELECT @command = 'IF EXISTS (SELECT 1 FROM sys.databases AS dbs LEFT JOIN ' + @SystemDatabase + '..SY01500 SY ON SY.INTERID = dbs.name WHERE dbs.name = ''?'' AND (dbs.name = ''' + @SystemDatabase + ''' OR SY.INTERID IS NOT NULL)) BEGIN USE [?]; INSERT INTO #ReturnedData (dbname, VIEWNAME) (SELECT DB_NAME() AS ''DB_NAME'', o.name FROM sys.objects AS o WHERE o.type = ''V'' AND o.name LIKE ''' + @View + '%'') END' EXEC sp_MSforeachdb @command SELECT * FROM #ReturnedData DROP TABLE #ReturnedData