如何在sql server数据库中找到常量的用法

时间:2010-01-10 18:50:55

标签: sql sql-server tsql search constants

有没有办法搜索(存储过程,函数,视图)的集合来使用常量?

我有一个问题,我有一个SQL服务器数据库。它声明了很多存储过程和函数。我正在寻找“115”的用法,这恰好是付费代码。我最初没有编写所有代码,因此我正在寻找声明常量的任何地方,或字面意思使用字符串“115”。

3 个答案:

答案 0 :(得分:7)

SELECT * FROM sys.objects WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%115%'

这包括在sys.sql_modules和sys.procedures

中省略的约束等

答案 1 :(得分:4)

您可以搜索sys.sql_modules definition LIKE N'%115%'

SELECT sm.object_id, OBJECT_NAME(sm.object_id) AS object_name
  , o.type, o.type_desc 
FROM sys.sql_modules AS sm
JOIN sys.objects AS o ON sm.object_id = o.object_id
WHERE sm.definition LIKE N'%115%'
ORDER BY o.type;

你可能会得到一堆误报,但至少你有一个起点。

答案 2 :(得分:0)

SQL Server 2000:

USE AdventureWorks
GO
--Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%Employee%'
GO
--Option 2
SELECT DISTINCT o.name ,o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%Employee%'
GO

SQL Server 2005:

USE AdventureWorks
GO
--Searching for Empoloyee table
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Employee%'
GO
--Searching for Empoloyee table and RateChangeDate column together
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Employee%'
AND OBJECT_DEFINITION(OBJECT_ID) LIKE '%RateChangeDate%'
GO

两个例子都取自:

http://blog.sqlauthority.com/2007/11/10/sql-server-2005-2000-search-string-in-stored-procedure/

类似地:

http://www.knowdotnet.com/articles/storedprocfinds.html

这个例子:

CREATE PROCEDURE Find_Text_In_SP
@StringToSearch varchar(100) 
AS 
   SET @StringToSearch = '%' +@StringToSearch + '%'
   SELECT Distinct SO.Name
   FROM sysobjects SO (NOLOCK)
   INNER JOIN syscomments SC (NOLOCK) on SO.Id = SC.ID
   AND SO.Type = 'P'
   AND SC.Text LIKE @stringtosearch
   ORDER BY SO.Name
GO