2011-12-27 167 views

回答

0

可能,該腳本將有利於你 -

DECLARE @SearchColumnName SYSNAME 
SELECT @SearchColumnName = 'EmployeeID' 

IF EXISTS(
    SELECT 1 
    FROM tempdb.dbo.sysobjects 
    WHERE id = OBJECT_ID('tempdb.dbo.#DB') 
) DROP TABLE #DB 

CREATE TABLE #DB 
(
     [DB_NAME] SYSNAME 
    , [OBJECT_NAME] SYSNAME 
    , [COLUMN_NAME] SYSNAME 
) 

DECLARE @SQL NVARCHAR(500) 
SELECT @SQL = 'USE [?] 

INSERT INTO #DB 
(
     [DB_NAME] 
    , [OBJECT_NAME] 
    , [COLUMN_NAME] 
) 
SELECT 
     DB_NAME() 
    , s.name + ''.'' + o.name 
    , c.name 
FROM (
    SELECT 
      c.[object_id] 
     , c.name 
    FROM sys.columns c WITH (NOWAIT) 
    WHERE c.name LIKE ''%'' + ''' + @SearchColumnName+ ''' + ''%'' 
) c 
JOIN sys.objects o WITH (NOWAIT) ON c.[object_id] = o.[object_id] 
JOIN sys.schemas s WITH (NOWAIT) ON o.[schema_id] = s.[schema_id] 
WHERE o.type = ''U''' 

EXEC sys.sp_MSforeachdb @SQL 

SELECT * 
FROM #DB 
ORDER BY [DB_NAME], [OBJECT_NAME]