SQL Server - where is “sys.functions”?

后端 未结 10 1287
天命终不由人
天命终不由人 2020-12-04 09:15

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

相关标签:
10条回答
  • 2020-12-04 09:53

    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
    
    0 讨论(0)
  • 2020-12-04 09:59

    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.

    0 讨论(0)
  • 2020-12-04 09:59

    try this :

    SELECT * FROM sys.objects
    where type_desc = 'SQL_SCALAR_FUNCTION'
    
    0 讨论(0)
  • 2020-12-04 09:59

    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.

    0 讨论(0)
  • 2020-12-04 10:01

    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

    0 讨论(0)
  • 2020-12-04 10:05

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

    select * from sys.objects where type_desc like '%fun%'
    
    0 讨论(0)
提交回复
热议问题