50

我正在寻找一个 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 过程或有目的的帮助程序这样的东西更容易,我会这样做并发布它。

4

5 回答 5

81
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%'
于 2013-03-14T21:26:47.073 回答
8

您可以使用 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
于 2013-03-14T21:16:35.453 回答
5
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')
于 2017-06-06T15:12:27.407 回答
3

与此解决方案类似: 查询以列出所有存储过程

SELECT * 
  FROM DIDS0100.INFORMATION_SCHEMA.ROUTINES
 WHERE ROUTINE_TYPE = 'FUNCTION' 
    AND LEFT(ROUTINE_NAME, 3) NOT IN ('fn_')
于 2020-02-25T15:45:55.487 回答
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
于 2021-06-14T13:36:41.160 回答