Quick way to search the existence of an object in sql server 2005.
Create a stored procedure by name 'sp_ObjectSearch' and copy the below code.Now start searching...
ex:-
sp_ObjectSearch 'Employee'
sp_ObjectSearch 'F4211' etc
USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_ObjectSearch] Script Date: 06/23/2009 05:10:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ==========================================================================================
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- ==========================================================================================
-- Script Title : sp_ObjectSearch
-- =====================================================================================
-- Description : Have you ever tried to look for an object in a
-- SQL Server instance that has hundreds of databases
-- without knowing the object's exact name and the
-- database in which it resides?
-- this proc will look for anyobject with the exisiting instance
-- that match all or part of the search string
-- =====================================================================================
-- Coder : Shaunt Khaldtiance
-- Creation Date : 02.09.2008
-- Last Modification Date :
--
-- example : exec master..sp_ObjectSearch 'user'
-- ==========================================================================================
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- ==========================================================================================
ALTER proc [dbo].[sp_ObjectSearch]
@object varchar(max)
as
declare dblist cursor for select name from sys.databases;
create table #result (DatabaseName varchar(256),TypeName varchar(256),ObjectName varchar(256))
declare @dbname varchar(max),
@cmd varchar(max)
open dblist
fetch next from dblist into @dbname
while (@@fetch_status=0)
begin
select @cmd='if exists (select * from ['+@dbname+'].sys.objects where name like ''%'+@object+'%'' )
begin
insert #result
select '''+upper(@dbname)+''' as [Database],
case type
when ''AF'' Then ''Aggregate function (CLR)''
when ''C'' Then ''CHECK constraint''
when ''D'' Then ''DEFAULT (constraint or stand-alone)''
when ''F'' Then ''FOREIGN KEY constraint''
when ''PK'' Then ''PRIMARY KEY constraint''
when ''P'' Then ''SQL stored procedure''
when ''PC'' Then ''Assembly (CLR) stored procedure''
when ''FN'' Then ''SQL scalar function''
when ''FS'' Then ''Assembly (CLR) scalar function''
when ''FT'' Then ''Assembly (CLR) table-valued function''
when ''R'' Then ''Rule (old-style, stand-alone)''
when ''RF'' Then ''Replication-filter-procedure''
when ''SN'' Then ''Synonym''
when ''SQ'' Then ''Service queue''
when ''TA'' Then ''Assembly (CLR) DML trigger''
when ''TR'' Then ''SQL DML trigger''
when ''IF'' Then ''SQL inlined table-valued function''
when ''TF'' Then ''SQL table-valued-function''
when ''U'' Then ''Table (user-defined)''
when ''UQ'' Then ''UNIQUE constraint''
when ''V'' Then ''View''
when ''X'' Then ''Extended stored procedure''
when ''IT'' Then ''Internal table''
end As Type
,Name as [Object Name] from ['+@dbname+'].sys.objects
where name like ''%'+@object+'%''
end '
exec (@cmd)
fetch next from dblist into @dbname
end
close dblist
deallocate dblist
select * from #result order by DatabaseName, TypeName, ObjectName
drop table #result
No comments:
Post a Comment