SQL Server - “sys.functions”在哪里?

发布于 2024-07-12 10:38:30 字数 312 浏览 14 评论 0原文

SQL Server 2005 在我经常使用的系统目录上有很棒的 sys.XXX 视图。

让我困惑的是:为什么有一个 sys.procedures 视图来查看有关存储过程的信息,但没有 sys.functions 视图来查看您的存储过程的信息存储函数?

没有人使用存储函数吗? 我发现它们对于例如计算列等非常方便!

缺少 sys.functions 是否有特定原因,或者只是认为它不够重要而无法放入 sys 目录视图中? 它在 SQL Server 2008 中可用吗?

SQL Server 2005 has great sys.XXX views on the system catalog which I use frequently.

What stumbles me is this: why is there a sys.procedures view to see info about your stored procedures, but there is no sys.functions view to see the same for your stored functions?

Doesn't anybody use stored functions? I find them very handy for e.g. computed columns and such!

Is there a specific reason sys.functions is missing, or is it just something that wasn't considered important enough to put into the sys catalog views? Is it available in SQL Server 2008?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

时光倒影 2024-07-19 10:38:30

我发现 UDF 非常方便,我一直在使用它们。

我不确定 Microsoft 在 SQL Server 2005(或 SQL Server 2008,据我所知)中不包含 sys.functions 等效项的理由是什么,但很容易推出您自己的:

CREATE VIEW my_sys_functions_equivalent
AS
SELECT *
FROM sys.objects
WHERE type IN ('FN', 'IF', 'TF')  -- scalar, inline table-valued, table-valued

I find UDFs are very handy and I use them all the time.

I'm not sure what Microsoft's rationale is for not including a sys.functions equivalent in SQL Server 2005 (or SQL Server 2008, as far as I can tell), but it's easy enough to roll your own:

CREATE VIEW my_sys_functions_equivalent
AS
SELECT *
FROM sys.objects
WHERE type IN ('FN', 'IF', 'TF')  -- scalar, inline table-valued, table-valued
与他有关 2024-07-19 10:38:30

列出函数的另一种方法是使用 INFORMATION_SCHEMA 视图。

SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION'

根据 Microsoft 网站,“信息架构视图提供了 SQL Server 元数据的内部、独立于系统表的视图。尽管对底层系统表进行了重大更改,但信息架构视图使应用程序能够正常工作”。 换句话说,随着 SQL 升级,底层系统表可能会发生变化,但视图仍应保持不变。

Another way to list functions is to make use of INFORMATION_SCHEMA views.

SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION'

According to the Microsoft web site "Information schema views provide an internal, system table-independent view of the SQL Server metadata. Information schema views enable applications to work correctly although significant changes have been made to the underlying system tables". In other words, the underlying System tables may change as SQL gets upgraded, but the views should still remain the same.

别念他 2024-07-19 10:38:30

根据 SSMS 在编写函数的 DROP 脚本时生成的内容,这在 2008 R2 中有效:

SELECT  *
FROM    sys.objects
WHERE   type IN (N'FN', N'IF', N'TF', N'FS', N'FT') ;

/*
From http://msdn.microsoft.com/en-us/library/ms177596.aspx:
 FN SQL_SCALAR_FUNCTION
 FS Assembly (CLR) scalar-function
 FT Assembly (CLR) table-valued function
 IF SQL_INLINE_TABLE_VALUED_FUNCTION
 TF SQL_TABLE_VALUED_FUNCTION
*/

This is valid in 2008 R2 per what SSMS generates when you script a DROP of a function:

SELECT  *
FROM    sys.objects
WHERE   type IN (N'FN', N'IF', N'TF', N'FS', N'FT') ;

/*
From http://msdn.microsoft.com/en-us/library/ms177596.aspx:
 FN SQL_SCALAR_FUNCTION
 FS Assembly (CLR) scalar-function
 FT Assembly (CLR) table-valued function
 IF SQL_INLINE_TABLE_VALUED_FUNCTION
 TF SQL_TABLE_VALUED_FUNCTION
*/
鯉魚旗 2024-07-19 10:38:30

它稍微冗长一些,但这应该做完全相同的事情:

select * from sys.objects where (type='TF' or type='FN')

据我所知,它也不在 SQL Server 2008 中。

It's very slightly more verbose, but this should do exactly the same thing:

select * from sys.objects where (type='TF' or type='FN')

As far as I can see, it's not in SQL Server 2008 either.

双马尾 2024-07-19 10:38:30

这并没有添加任何新内容,但我发现以下内容更容易记住:

select * from sys.objects where type_desc like '%fun%'

This does not add anything new, but I found the following easier to remember:

select * from sys.objects where type_desc like '%fun%'
苄①跕圉湢 2024-07-19 10:38:30

要扩展@LukeH的答案,要返回函数定义也需要连接到sys.sql_modules表。 所以对此的查询是:

SELECT O.name as 'Function name', M.definition as 'Definition', O.object_id
FROM sys.objects as O INNER JOIN sys.sql_modules as M
    ON O.object_id = M.object_id
WHERE type IN ('FN', 'IF', 'TF')  -- scalar, inline table-valued, table-valued

上面分别显示函数名称、其定义和对象标识符。

To extend upon @LukeH's answer, to return the function definitions as well requires a join to the sys.sql_modules table. So the query for this is:

SELECT O.name as 'Function name', M.definition as 'Definition', O.object_id
FROM sys.objects as O INNER JOIN sys.sql_modules as M
    ON O.object_id = M.object_id
WHERE type IN ('FN', 'IF', 'TF')  -- scalar, inline table-valued, table-valued

where the above displays the function name, its definition and the object identifier respectively.

小糖芽 2024-07-19 10:38:30

尝试这个 :

SELECT * FROM sys.objects
where type_desc = 'SQL_SCALAR_FUNCTION'

try this :

SELECT * FROM sys.objects
where type_desc = 'SQL_SCALAR_FUNCTION'
机场等船 2024-07-19 10:38:30

有关标量函数(包括所有者和返回类型)的更完整描述:

SELECT f.name, s.name AS owner, t.name as dataType, p.max_length, p.precision, p.scale, m.definition
FROM sys.objects f
JOIN sys.schemas s on s.schema_id = f.schema_id
JOIN sys.parameters p on p.object_id = f.object_id AND p.parameter_id = 0
JOIN sys.types t ON t.system_type_id = p.system_type_id 
JOIN sys.sql_modules as m ON m.object_id = f.object_id
WHERE type='FN';

For a fuller description of scalar functions including owner and return type:

SELECT f.name, s.name AS owner, t.name as dataType, p.max_length, p.precision, p.scale, m.definition
FROM sys.objects f
JOIN sys.schemas s on s.schema_id = f.schema_id
JOIN sys.parameters p on p.object_id = f.object_id AND p.parameter_id = 0
JOIN sys.types t ON t.system_type_id = p.system_type_id 
JOIN sys.sql_modules as m ON m.object_id = f.object_id
WHERE type='FN';
云裳 2024-07-19 10:38:30

顺便说一句,您不想包含 type = 'FS' 吗?

name    type    type_desc
getNewsletterStats  FS  CLR_SCALAR_FUNCTION

这就是 sys.objects 中的项目对应于我的 UDF,它是从外部 DLL 派生的

incidentally, wouldn't you want to include type = 'FS'?

name    type    type_desc
getNewsletterStats  FS  CLR_SCALAR_FUNCTION

that's what the item in sys.objects corresponds with for my UDF which is derived from an external DLL

落在眉间の轻吻 2024-07-19 10:38:30

SQL 2000 特定,对象名称略有调整:

SELECT *
FROM sysobjects
WHERE type IN ('FN', 'IF', 'TF')

OR

SELECT *
FROM dbo.sysobjects
WHERE type IN ('FN', 'IF', 'TF')

SQL 2000 specific, slight adjustment for the object name:

SELECT *
FROM sysobjects
WHERE type IN ('FN', 'IF', 'TF')

OR

SELECT *
FROM dbo.sysobjects
WHERE type IN ('FN', 'IF', 'TF')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文