sql-servertable-valued-parameters

Query to List Table-Valued Functions and their Input Arguments?


The following query will return a list of all table-valued functions in a database and a list of their output columns (and column data types).

USE DATABASENAME
GO

SELECT
rc.*
FROM
INFORMATION_SCHEMA.ROUTINES r INNER JOIN
INFORMATION_SCHEMA.ROUTINE_COLUMNS rc ON
r.ROUTINE_CATALOG = rc.TABLE_CATALOG AND
r.ROUTINE_SCHEMA = rc.TABLE_SCHEMA AND
r.ROUTINE_NAME = rc.TABLE_NAME
WHERE
r.ROUTINE_TYPE = 'FUNCTION' AND
r.DATA_TYPE = 'TABLE'

But is there a way to query for a list all table-valued functions in a database along with a list of their input arguments (and argument data types)?

I looked here but didn't find a solution.

I noticed that there is a column named ROUTINE_DEFINITION in the INFORMATION_SCHEMA.ROUTINES table which shows the full script that created the table-valued function, so I suppose it is possible to parse through this string and find the input data types that you're looking for, although this would be very complicated especially considering the programming comments are included in this ROUTINE_DEFINITION which only further complicates things.


Solution

  • INFORMATION_SCHEMA system tables should be avoided, as they are generalized views for non-specific-to-SQL-Server clients. Instead use the sys schema's tables and views.

    Unfortunately there is no sys.functions so we have to filter sys.objects instead:

    select o.name, p.*, t.name as type_name
    from sys.parameters p
    join sys.types t on t.user_type_id = p.user_type_id
    join sys.objects o on o.object_id = p.object_id
    where o.[type] in ('IF', 'TF') -- IF is inline, TF is multi-statement