数据库中所有用户定义函数的SQL列表

时间:2013-03-14 21:08:07

标签: sql-server sql-server-2008-r2

我正在寻找一个SQL查询,它输出数据库目录中所有用户定义函数的函数定义。

我找到了

SELECT OBJECT_DEFINITION (OBJECT_ID(N'dbo.UserFunctionName')) AS [Object Definition]

SELECT ROUTINE_NAME FROM information_schema.routines WHERE routine_type = 'function'

但我无法想到或找到将ROUTINE_NAME列表提供给OBJECT_ID的方法。

这里的目的是在数据库中用于数据库变更分析的用户定义函数定义的可搜索文本,如果像完整的SQL过程或有用的帮助程序更容易,我会这样做并发布它。

5 个答案:

答案 0 :(得分:59)

SELECT name, definition, type_desc 
  FROM sys.sql_modules m 
INNER JOIN sys.objects o 
        ON m.object_id=o.object_id
WHERE type_desc like '%function%'

答案 1 :(得分:7)

您可以使用CTE:

with functions(routine_name) as 
  (SELECT ROUTINE_NAME FROM information_schema.routines WHERE routine_type = 'function')
select 
  OBJECT_DEFINITION(OBJECT_ID(routine_name)) AS [Object Definition] 
from 
  functions

答案 2 :(得分:3)

SELECT O.name, M.definition, O.type_desc, O.type
FROM sys.sql_modules M
INNER JOIN sys.objects O ON M.object_id=O.object_id
WHERE O.type IN ('IF','TF','FN')

答案 3 :(得分:0)

类似于此解决方案:Query to list all stored procedures

SELECT * 
  FROM DIDS0100.INFORMATION_SCHEMA.ROUTINES
 WHERE ROUTINE_TYPE = 'FUNCTION' 
    AND LEFT(ROUTINE_NAME, 3) NOT IN ('fn_')

答案 4 :(得分:0)

这是一个包含模式的版本,并且被格式化为允许大量删除不需要的标量值函数:

SELECT ('DROP FUNCTION [' + SCHEMA_NAME(o.schema_id) + '].[' + o.name + ']') AS ObjectName 
FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id = o.object_id 
WHERE o.type_desc = 'SQL_SCALAR_FUNCTION' 
ORDER BY ObjectName