我想从我的所有数据库存储过程中搜索文本。我使用下面的SQL:
SELECT DISTINCT
o.name AS Object_Name,
o.type_desc
FROM sys.sql_modules m
INNER JOIN
sys.objects o
ON m.object_id = o.object_id
WHERE m.definition Like '%[ABD]%';
我想在包括方括号在内的所有存储过程中搜索[ABD]
,但它没有给出正确的结果。如何更改查询以实现此目的?
答案 0 :(得分:506)
转出方括号:
...
WHERE m.definition Like '%\[ABD\]%' ESCAPE '\'
然后方括号将被视为字符串文字,而不是外卡。
答案 1 :(得分:273)
尝试此请求:
<强>查询强>
SELECT name
FROM sys.procedures
WHERE Object_definition(object_id) LIKE '%strHell%'
答案 2 :(得分:54)
您是否尝试过使用某些第三方工具进行搜索?有几个是免费的,过去我节省了很多时间。
以下是我使用过的两个SSMS Addins。
ApexSQL Search - 搜索数据库中的架构和数据,并具有其他功能,例如依赖关系跟踪等......
SSMS Tools pack - 具有与之前版本相同的搜索功能以及其他一些很酷的功能。 SQL Server 2012不是免费的,但仍然非常实惠。
我知道这个答案并非与问题100%相关(更具体),但希望其他人会发现这个问题很有用。
答案 3 :(得分:32)
我通常运行以下操作来实现:
select distinct object_name(id)
from syscomments
where text like '%[ABD]%'
order by object_name(id)
答案 4 :(得分:22)
Redgate's SQL Search是一个很好的工具,它是SSMS的免费插件。
答案 5 :(得分:21)
使用SQL Server的好习惯。
制作以下商店程序并为底部图像设置短按,
CREATE PROCEDURE [dbo].[Searchinall]
(@strFind AS VARCHAR(MAX))
AS
BEGIN
SET NOCOUNT ON;
--TO FIND STRING IN ALL PROCEDURES
BEGIN
SELECT OBJECT_NAME(OBJECT_ID) SP_Name
,OBJECT_DEFINITION(OBJECT_ID) SP_Definition
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%'
END
--TO FIND STRING IN ALL VIEWS
BEGIN
SELECT OBJECT_NAME(OBJECT_ID) View_Name
,OBJECT_DEFINITION(OBJECT_ID) View_Definition
FROM sys.views
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%'
END
--TO FIND STRING IN ALL FUNCTION
BEGIN
SELECT ROUTINE_NAME Function_Name
,ROUTINE_DEFINITION Function_definition
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%'+@strFind+'%'
AND ROUTINE_TYPE = 'FUNCTION'
ORDER BY
ROUTINE_NAME
END
--TO FIND STRING IN ALL TABLES OF DATABASE.
BEGIN
SELECT t.name AS Table_Name
,c.name AS COLUMN_NAME
FROM sys.tables AS t
INNER JOIN sys.columns c
ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%'+@strFind+'%'
ORDER BY
Table_Name
END
END
现在 - 设置如下所示的短键,
因此,每当您想要在Store procedure
,Views
,Functions
和Tables
这四个对象中的任何一个中查找特定文本时,下次。您只需要编写该关键字并按下快捷键。
例如:我想搜索&#39; PaymentTable&#39;然后写下“支付表”&#39;在查询编辑器中按下快捷键ctrl+4
- 它将为您提供完整的结果。
答案 6 :(得分:14)
请将此作为&#34;脏&#34;替代但这特别是在我不熟悉数据库项目的情况下,多次挽救了我。有时你试图在所有SP中搜索一个字符串,并忘记某些相关逻辑可能隐藏在函数和触发器之间,或者它的措辞可能与你想象的不同。
您可以在MSSMS上右键单击数据库并选择
private static native void unwrapAccordionGroup() /*-{
$wnd.jQuery('#test').unwrap()
}-*/;
向导将所有SP,Fns和触发器输出到单个.sql文件中。
确保选择触发器!
然后只需使用Sublime或记事本来搜索您需要查找的字符串。 我知道这可能是非常低效和偏执的方法,但它有效:)
答案 7 :(得分:12)
你也可以使用这个:
SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION like '%Search_String%'
答案 8 :(得分:7)
select top 10 * from
sys.procedures
where object_definition(object_id) like '%\[ABD\]%'
答案 9 :(得分:6)
SELECT DISTINCT
o.name AS Object_Name,
o.type_desc
FROM sys.sql_modules m INNER JOIN sys.objects o
ON m.object_id = o.object_id WHERE m.definition Like '%[String]%';
答案 10 :(得分:6)
它可能对你有帮助!
SELECT DISTINCT
A.NAME AS OBJECT_NAME,
A.TYPE_DESC
FROM SYS.SQL_MODULES M
INNER JOIN SYS.OBJECTS A ON M.OBJECT_ID = A.OBJECT_ID
WHERE M.DEFINITION LIKE '%['+@SEARCH_TEXT+']%'
ORDER BY TYPE_DESC
答案 11 :(得分:5)
您也可以使用:
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE '%flags.%'
AND OBJECTPROPERTY(id, 'IsProcedure') = 1
GROUP BY OBJECT_NAME(id)
这包括评论
答案 12 :(得分:3)
使用CHARINDEX:
SELECT DISTINCT o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id=o.object_id
WHERE CHARINDEX('[ABD]',m.definition) >0 ;
使用PATINDEX:
SELECT DISTINCT o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id=o.object_id
WHERE PATINDEX('[[]ABD]',m.definition) >0 ;
使用此双[[]ABD]
类似于转义:
WHERE m.definition LIKE '%[[]ABD]%'
答案 13 :(得分:3)
我创建了一个在程序,表格,视图或作业中搜索文本的过程。第一个参数@search是搜索条件,@ target是搜索目标,即过程,表等。如果未指定,则搜索全部。 @db是指定要搜索的数据库,如果未指定,则使用当前的db。这是动态SQL中的查询。
ALTER PROCEDURE [dbo].[usp_find_objects]
(
@search VARCHAR(255),
@target VARCHAR(255) = NULL,
@db VARCHAR(35) = NULL
)
AS
SET NOCOUNT ON;
DECLARE @TSQL NVARCHAR(MAX), @USEDB NVARCHAR(50)
IF @db <> '' SET @USEDB = 'USE ' + @db
ELSE SET @USEDB = ''
IF @target IS NULL SET @target = ''
SET @TSQL = @USEDB + '
DECLARE @search VARCHAR(128)
DECLARE @target VARCHAR(128)
SET @search = ''%' + @search + '%''
SET @target = ''' + @target + '''
IF @target LIKE ''%Procedure%'' BEGIN
SELECT o.name As ''Stored Procedures''
FROM SYSOBJECTS o
INNER JOIN SYSCOMMENTS c ON o.id = c.id
WHERE c.text LIKE @search
AND o.xtype = ''P''
GROUP BY o.name
ORDER BY o.name
END
ELSE IF @target LIKE ''%View%'' BEGIN
SELECT o.name As ''Views''
FROM SYSOBJECTS o
INNER JOIN SYSCOMMENTS c ON o.id = c.id
WHERE c.text LIKE @search
AND o.xtype = ''V''
GROUP BY o.name
ORDER BY o.name
END
/* Table - search table name only, need to add column name */
ELSE IF @target LIKE ''%Table%'' BEGIN
SELECT t.name AS ''TableName''
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE @search
ORDER BY TableName
END
ELSE IF @target LIKE ''%Job%'' BEGIN
SELECT j.job_id,
s.srvname,
j.name,
js.step_id,
js.command,
j.enabled
FROM [msdb].dbo.sysjobs j
JOIN [msdb].dbo.sysjobsteps js
ON js.job_id = j.job_id
JOIN master.dbo.sysservers s
ON s.srvid = j.originating_server_id
WHERE js.command LIKE @search
END
ELSE BEGIN
SELECT o.name As ''Stored Procedures''
FROM SYSOBJECTS o
INNER JOIN SYSCOMMENTS c ON o.id = c.id
WHERE c.text LIKE @search
AND o.xtype = ''P''
GROUP BY o.name
ORDER BY o.name
SELECT o.name As ''Views''
FROM SYSOBJECTS o
INNER JOIN SYSCOMMENTS c ON o.id = c.id
WHERE c.text LIKE @search
AND o.xtype = ''V''
GROUP BY o.name
ORDER BY o.name
SELECT t.name AS ''Tables''
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE @search
ORDER BY Tables
SELECT j.name AS ''Jobs''
FROM [msdb].dbo.sysjobs j
JOIN [msdb].dbo.sysjobsteps js
ON js.job_id = j.job_id
JOIN master.dbo.sysservers s
ON s.srvid = j.originating_server_id
WHERE js.command LIKE @search
END
'
EXECUTE sp_executesql @TSQL
答案 14 :(得分:2)
也试试这个:
SELECT ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION like '%\[ABD\]%'
答案 15 :(得分:2)
SELECT DISTINCT OBJECT_NAME([id])--,syscomments.* this gives name of
--stored procedures that text found
FROM syscomments
WHERE [id] IN (SELECT [id] FROM sysobjects WHERE xtype IN
('TF','FN','V','P') AND status >= 0) AND
([text] LIKE '%text to be search%' )
OBJECT_NAME([id]) - &gt;对象名称(视图,存储过程,标量函数,表函数名称)
id(int)=对象标识号
xtype char(2)对象类型。可以是以下对象类型之一:
FN =标量函数
P =存储过程
V =查看
TF =表函数
答案 16 :(得分:1)
Select distinct OBJECT_NAME(id) from syscomments where text like '%string%' AND OBJECTPROPERTY(id, 'IsProcedure') = 1
答案 17 :(得分:0)
/*
SEARCH SPROCS & VIEWS
The following query will allow search within the definitions
of stored procedures and views.
It spits out the results as XML, with the full definitions,
so you can browse them without having to script them individually.
*/
/*
STEP 1: POPULATE SEARCH KEYS. (Set to NULL to ignore)
*/
DECLARE
@def_key varchar(128) = '%foo%', /* <<< definition search key */
@name_key varchar(128) = '%bar%', /* <<< name search key */
@schema_key varchar(128) = 'dbo'; /* <<< schema search key */
;WITH SearchResults AS (
/*
STEP 2: DEFINE SEARCH QUERY AS CTE (Common Table Expression)
*/
SELECT
[Object].object_id AS [object_id],
[Schema].name AS [schema_name],
[Object].name AS [object_name],
[Object].type AS [object_type],
[Object].type_desc AS [object_type_desc],
[Details].definition AS [module_definition]
FROM
/* sys.sql_modules = where the body of sprocs and views live */
sys.sql_modules AS [Details] WITH (NOLOCK)
JOIN
/* sys.objects = where the metadata for every object in the database lives */
sys.objects AS [Object] WITH (NOLOCK) ON [Details].object_id = [Object].object_id
JOIN
/* sys.schemas = where the schemas in the datatabase live */
sys.schemas AS [Schema] WITH (NOLOCK) ON [Object].schema_id = [Schema].schema_id
WHERE
(@def_key IS NULL OR [Details].definition LIKE @def_key) /* <<< searches definition */
AND (@name_key IS NULL OR [Object].name LIKE @name_key) /* <<< searches name */
AND (@schema_key IS NULL OR [Schema].name LIKE @schema_key) /* <<< searches schema */
)
/*
STEP 3: SELECT FROM CTE INTO XML
*/
/*
This outer select wraps the inner queries in to the <sql_object> root element
*/
SELECT
(
/*
This inner query maps stored procedure rows to <procedure> elements
*/
SELECT TOP 100 PERCENT
[object_id] AS [@object_id],
[schema_name] + '.' + [object_name] AS [@full_name],
[module_definition] AS [module_definition]
FROM
SearchResults
WHERE
object_type = 'P'
ORDER BY
[schema_name], [object_name]
FOR XML
PATH ('procedure'), TYPE
) AS [procedures], /* <<< as part of the outer query,
this alias causes the <procedure> elements
to be wrapped within the <procedures> element */
(
/*
This inner query maps view rows to <view> elements
*/
SELECT TOP 100 PERCENT
[object_id] AS [@object_id],
[schema_name] + '.' + [object_name] AS [@full_name],
[module_definition] AS [module_definition]
FROM
SearchResults
WHERE
object_type = 'V'
ORDER BY
[schema_name], [object_name]
FOR XML
PATH ('view'), TYPE
) AS [views] /* <<< as part of the outer query,
this alias causes the <view> elements
to be wrapped within the <views> element */
FOR XML
PATH ('sql_objects')
答案 18 :(得分:0)
不同的版本,使查询更适合不同的编码实践。
SELECT DISTINCT
O.NAME AS OBJECT_NAME,
O.TYPE_DESC
FROM SYS.SQL_MODULES M
INNER JOIN
SYS.OBJECTS O
ON M.OBJECT_ID = O.OBJECT_ID
WHERE UPPER(M.DEFINITION) LIKE UPPER('%Your Text%');
答案 19 :(得分:0)
此查询是来自所有数据库的存储过程中的搜索文本。
#include <stdio.h>
#include <stdlib.h>
struct guy{
char name[50];
char code[15];
};
struct guy vet[2];
int i, j;
int registerGuy(){
for(i=0; i<2; i++){
printf("Insert your name:");
gets(vet[i].name);
fflush(stdin);
printf("Insert your code:");
gets(vet[i].code);
fflush(stdin);
for(j=0; j<2; j++){
if(strcmp(vet[i].code,vet[j].code) == 0){
printf("Error. Code already registered.");
break;
}
}
}
for(i=0; i<2; i++){
printf("Name: %s \n", vet[i].name);
printf("Code: %s \n", vet[i].code);
}
}
int main(int argc, char *argv[]) {
registerGuy();
return 0;
}
答案 20 :(得分:0)
-- Applicable for SQL 2005+
USE YOUR_DATABASE_NAME //;
GO
SELECT [Scehma] = schema_name(o.schema_id)
,o.NAME
,o.type
FROM sys.sql_modules m
INNER JOIN sys.objects o ON o.object_id = m.object_id
WHERE m.DEFINITION LIKE '%YOUR SEARCH KEYWORDS%'
GO
答案 21 :(得分:0)
每隔一段时间我就会使用这个脚本来确定要修改哪些proc,或者找出使用表的列的内容,或者根本用来删除一些旧的垃圾。它通过精彩提供的sp_msforeachdb检查它运行的实例上的每个数据库。
if object_id('tempdb..##nothing') is not null
drop table ##nothing
CREATE TABLE ##nothing
(
DatabaseName varchar(30),
SchemaName varchar(30),
ObjectName varchar(100),
ObjectType varchar(50)
)
EXEC master.sys.sp_msforeachdb
'USE ?
insert into ##nothing
SELECT
db_name() AS [Database],
[Scehma]=schema_name(o.schema_id),
o.Name,
o.type
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON o.object_id = m.object_id
WHERE
m.definition like ''%SOME_TEXT%'''
--edit this text
SELECT * FROM ##nothing n
order by OBJECTname
答案 22 :(得分:-1)
SELECT name , type_desc , create_date , modify_date
FROM sys.procedures
WHERE Object_definition(object_id) LIKE '%High%'
答案 23 :(得分:-2)
您也可以使用
CREATE PROCEDURE [Search](
@Filter nvarchar(max)
)
AS
BEGIN
SELECT name
FROM procedures
WHERE definition LIKE '%'+@Filter+'%'
END
然后运行
exec [Search] 'text'