我正在使用Microsoft SQL Server Management Studio。我想找到视图的位置或具有特定名称的表:vw_found
,并且这在包含许多数据库的服务器中。
希望你能帮助我。
答案 0 :(得分:0)
<强> sp_MSforeachdb 强>
EXECUTE master.sys.sp_MSforeachdb 'USE [?]; SELECT DB_NAME(), * FROM sys.objects WHERE Name = ''vw_found'''
答案 1 :(得分:0)
BEGIN
DECLARE @CurrentDatabaseId INT = 0
DECLARE @MaxDatabaseId INT = (SELECT
MAX(database_id)
FROM sys.databases d)
DECLARE @CheckTableName VARCHAR(100) = '<TableName>'
DECLARE @CheckSchemaName VARCHAR(100) = '<SchemaName>'
WHILE @MaxDatabaseId >= @CurrentDatabaseId
BEGIN
IF EXISTS (SELECT
*
FROM sys.databases d
WHERE d.database_id = @CurrentDatabaseId)
BEGIN
DECLARE @CurrentDBName VARCHAR(100);
DECLARE @Sql NVARCHAR(MAX);
DECLARE @RowCount INT;
SELECT
@CurrentDBName = d.name
FROM sys.databases d
WHERE d.database_id = @CurrentDatabaseId
SET @Sql = 'SELECT @RowCnt = COUNT(*) FROM ' + @CurrentDBName + '.INFORMATION_SCHEMA.TABLES t WHERE t.TABLE_SCHEMA = ''' + @CheckSchemaName + '''' + ' AND t.TABLE_NAME = ''' + @CheckTableName + ''''
EXEC sp_executesql @Sql, N'@RowCnt INT OUTPUT', @RowCnt= @RowCount OUTPUT
IF @RowCount > 0
BEGIN
PRINT 'Table : ' + @CheckTableName + ' Exists in Database :' + @CurrentDBName
END
END
SET @CurrentDatabaseId = @CurrentDatabaseId + 1
END END