Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search text in all database objects in MS SQL Server

I am able search in procedures and triggers for exact text within the "" . But i need to search a string within the %% for all phrases in all database objects


2 Answers

select * from sys.sql_modules 
where definition like '%test%'

some info about SYS.SQL_Modules

Returns a row for each object that is an SQL language-defined module in SQL Server, including natively compiled scalar user-defined function. Objects of type P, RF, V, TR, FN, IF, TF, and R have an associated SQL module

with sp_msfporeachDB, you can test it in all databases;

EXECUTE master.sys.sp_MSforeachdb 'USE [?]; 

if db_id()>4'
Begin
select * from sys.sql_modules where defintion like ''%test%''
end
like image 119
TheGameiswar Avatar answered Oct 26 '25 23:10

TheGameiswar


SELECT ROUTINE_TYPE,* FROM INFORMATION_SCHEMA.ROUTINES WHERE 
CHARINDEX( 'test',ROUTINE_DEFINITION)>0

SELECT VIEW_DEFINITION,* FROM INFORMATION_SCHEMA.VIEWS WHERE 
CHARINDEX('test',VIEW_DEFINITION)>0

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE 
(CHARINDEX( 'col1',COLUMN_NAME)>0 OR CHARINDEX( 'tab2',TABLE_NAME)>0)

Instead of like operator with Percentile (Like '%test%') we used charindex to improve the performance of search

like image 28
Srini Avatar answered Oct 26 '25 21:10

Srini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!