我可以在""
中搜索确切文本的过程和触发器。
但我需要在%%
内搜索所有数据库对象中所有短语的字符串
答案 0 :(得分:3)
select * from sys.sql_modules
where definition like '%test%'
有关SYS.SQL_Modules
的一些信息为每个对象返回一行,该对象是SQL Server中SQL语言定义的模块,包括本机编译的标量用户定义函数。 P,RF,V,TR,FN,IF,TF和R类型的对象具有关联的SQL模块
使用sp_msfporeachDB,您可以在所有数据库中测试它;
EXECUTE master.sys.sp_MSforeachdb 'USE [?];
if db_id()>4'
Begin
select * from sys.sql_modules where defintion like ''%test%''
end
答案 1 :(得分:0)
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)
我们使用charindex来改善搜索性能,而不是像Percentile那样的运营商(比如'%test%')
答案 2 :(得分:-1)
这是一个重复的问题......答案就在这里.. How to find a string inside a entire database?
DECLARE @MyValue NVarChar(4000) = 'something';
SELECT S.name SchemaName, T.name TableName
INTO #T
FROM sys.schemas S INNER JOIN
sys.tables T ON S.schema_id = T.schema_id;
WHILE (EXISTS (SELECT * FROM #T)) BEGIN
DECLARE @SQL NVarChar(4000) = 'SELECT * FROM $$TableName WHERE (0 = 1) ';
DECLARE @TableName NVarChar(1000) = (
SELECT TOP 1 SchemaName + '.' + TableName FROM #T
);
SELECT @SQL = REPLACE(@SQL, '$$TableName', @TableName);
DECLARE @Cols NVarChar(4000) = '';
SELECT
@Cols = COALESCE(@Cols + 'OR CONVERT(NVarChar(4000), ', '') + C.name + ') = CONVERT(NVarChar(4000), ''$$MyValue'') '
FROM sys.columns C
WHERE C.object_id = OBJECT_ID(@TableName);
SELECT @Cols = REPLACE(@Cols, '$$MyValue', @MyValue);
SELECT @SQL = @SQL + @Cols;
EXECUTE(@SQL);
DELETE FROM #T
WHERE SchemaName + '.' + TableName = @TableName;
END;
DROP TABLE #T;