SQL Server - where is "sys.functions"?

SqlSql ServerSql Server-2005Stored Functions

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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

Solution 2 - 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.

Solution 3 - Sql

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
*/

Solution 4 - Sql

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.

Solution 5 - Sql

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

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

Solution 6 - Sql

try this :

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

Solution 7 - Sql

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

Solution 8 - Sql

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.

Solution 9 - Sql

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';

Solution 10 - Sql

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')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmarc_sView Question on Stackoverflow
Solution 1 - SqlLukeHView Answer on Stackoverflow
Solution 2 - SqlTim CView Answer on Stackoverflow
Solution 3 - SqlOrlando ColamatteoView Answer on Stackoverflow
Solution 4 - SqlAyresomeView Answer on Stackoverflow
Solution 5 - SqlyoniLaviView Answer on Stackoverflow
Solution 6 - Sqlvishal kadamView Answer on Stackoverflow
Solution 7 - SqlkramaswamyView Answer on Stackoverflow
Solution 8 - SqlMoonKnightView Answer on Stackoverflow
Solution 9 - SqlPeter BrandView Answer on Stackoverflow
Solution 10 - SqlGoran B.View Answer on Stackoverflow