查看某个对象在哪些库中存在

USE Master
GO

/**********************************************************
**ServerName:         N/A
**DatabaseNAME:   Master
**ObjectName:         up_SearchObject
**Function:                查找某个对象所在的数据库和表,对象为除了索引之外的任何对象,包括表,视图,存储过程                                等,若不存在,返回无结果
**Author:                    SQLServer_2005
**Date:                       2006.9.7
**Version:                  V1.0
*********************************************************/

ALTER PROCEDURE dbo.up_SearchObject
@ObjectName sysname
AS
EXEC('CREATE TABLE Temp..#(
                            ID int identity(1,1),
                            ObjectName sysname,
       ObjectType varchar(30),
       CreateTime datetime,
       DatabaseName sysname,
       TableName varchar(256)
                           )
 INSERT INTO Temp..#(ObjectName,ObjectType,CreateTime,DatabaseName,TableName) EXEC SP_MSFOREACHDB
@COMMAND1=''IF EXISTS(
                      SELECT 1
                      FROM ?..sysobjects
                      WHERE name = '''''+@ObjectName+'''''
                    )
           BEGIN       
            SELECT name AS ObjectName,
                            CASE WHEN xtype = ''''C'''' THEN ''''Check''''
                                 WHEN xtype = ''''D'''' THEN ''''Default''''
                                 WHEN xtype = ''''F'''' THEN ''''Foreign Key''''
                                 WHEN xtype = ''''L'''' THEN ''''Log''''
                                 WHEN xtype = ''''FN'''' THEN '''' Function''''
                                 WHEN xtype = ''''IN'''' THEN '''' Inner Talbe Function''''
                                 WHEN xtype = ''''P'''' THEN '''' Procedure''''
                                 WHEN xtype = ''''PK'''' THEN '''' Primary Key''''
                                 WHEN xtype = ''''RF'''' THEN '''' Replication Procedure''''
                                 WHEN xtype = ''''S'''' THEN '''' System Table''''
                                 WHEN xtype = ''''TF'''' THEN '''' Table Function''''
                                 WHEN xtype = ''''TR'''' THEN '''' Trigger''''
                                 WHEN xtype = ''''U'''' THEN '''' User Table''''
                                 WHEN xtype = ''''UQ'''' THEN '''' Unique''''
                                 WHEN xtype = ''''V'''' THEN '''' View''''
                                 WHEN xtype = ''''X'''' THEN '''' Extend Procedure''''
                                 ELSE ''''Unknown''''
                            END AS ObjectType,
                            CONVERT(VARCHAR(20),crdate,120) AS CreateTime,
                            ''''?'''' AS DatabaseName,
                            TableName=ISNULL((SELECT TOP 1 name FROM ?..sysobjects WHERE id = obj.parent_obj),''''N/A'''')
                FROM ?..sysobjects obj
                WHERE NAME = '''''+@ObjectName+'''''
           END''

    SELECT ID,ObjectName,ObjectType,CreateTime,DatabaseName,TableName
    FROM Temp..#

           TRUNCATE TABLE Temp..#
           DROP TABLE Temp..#
')


/**调用
EXEC up_SearchObject 'T1'
**/ 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值