2. Viewing and Modifying User-Defined Functions
Besides
using T-SQL commands to create functions, you can also use them to view
and modify functions. You can get information by using the provided
system procedures and queries against the INFORMATION_SCHEMA.routines view. The following sections describe these methods.
Using T-SQL to View Functions
To view the source code for a user-defined function, you can use the sp_helptext procedure:
use bigpubs2008
go
exec sp_helptext getonlydate
go
Text
------------------------------------------------------------------------------
CREATE FUNCTION getonlydate ()
RETURNS datetime
AS
BEGIN RETURN (select convert(datetime, convert(date, getdate())))
END
Note
To display the source code for the functions clearly,
configure the SSMS query window to display results as text rather than
in the grid by pressing Ctrl+T.
In addition to sp_helptext, you can write queries against the INFORMATION_SCHEMA.routines view to display the source code for a function:
SELECT routine_definition
from INFORMATION_SCHEMA.routines
where routine_name = 'getonlydate'
and specific_schema = 'dbo'
and specific_catalog = 'bigpubs2008'
routine_definition
------------------------------------------------------------------------------
CREATE FUNCTION getonlydate ()
RETURNS datetime
AS
BEGIN RETURN (select convert(datetime, convert(date, getdate())))
END
If you want to display information about the input parameters for a function, you use the INFORMATION_SCHEMA.parameters view. For scalar functions, the view also displays information for the return parameter, which has an ordinal position of 0 and no parameter name:
select substring(parameter_name,1,20) as parameter_name,
substring(data_type, 1, 20) as data_type,
Parameter_mode,
ordinal_position
from INFORMATION_SCHEMA.parameters
where specific_name = 'striptime'
and specific_schema = 'dbo'
and specific_catalog = 'bigpubs2008'
order by ordinal_position
go
parameter_name data_type Parameter_mode ordinal_position
-------------------- -------------------- -------------- ----------------
datetime OUT 0
@datetimeval datetime IN 1
If you want to display information about the result columns returned by a table-valued function, use the INFORMATION_SCHEMA.routine_columns view:
select substring(column_name, 1, 20) as column_name,
substring (data_type, 1, 12)
+ case when character_maximum_length is not null
then '(' + cast(character_maximum_length as varchar(4)) + ')'
else ''
end
as datatype,
numeric_precision as 'precision',
numeric_scale as scale,
ordinal_position
from INFORMATION_SCHEMA.routine_columns
where table_name = 'AveragePricebyType'
order by ordinal_position
go
column_name datatype precision scale ordinal_position
-------------------- ------------------ --------- ----------- ----------------
type char(12) NULL NULL 1
avg_price money 19 4 2
In addition, SQL Server provides the OBJECTPROPERTY
function, which you can use to get information about functions. One of
the things you can find out is whether a function is a multistatement
table function, an inline function, or a scalar function. The OBJECTPROPERTY function accepts an object ID and an object property parameter, and it returns the value 1 if the property is true, 0 if it is false, or NULL if an invalid function ID or property parameter is specified. The following property parameters are appropriate for functions:
IsTableFunction— Returns 1 if the function is a table-valued function but not an inline function.
IsInlineFunction— Returns 1 if the function is an inline table-valued function.
IsScalarFunction— Returns 1 if the function is a scalar function.
IsSchemaBound— Returns 1 if the function was created with the SCHEMABINDING option.
IsDeterministic— Returns 1
if the function is deterministic (that is, it always returns the same
result each time it is called with a specific set of input values).
The following example demonstrates a possible use of the OBJECTPROPERTY function with the INFORMATION_SCHEMA.routines view:
select convert(varchar(10), specific_Schema) as 'schema',
convert(varchar(20), specific_name) as 'function',
case objectproperty(object_id(specific_name), 'IsScalarFunction')
when 1 then 'Yes' else 'No' end as IsScalar,
case objectproperty(object_id(specific_name), 'IsTableFunction')
when 1 then 'Yes' else 'No' end as IsTable,
case objectproperty(object_id(specific_name), 'IsInlineFunction')
when 1 then 'Yes' else 'No' end as IsInline,
case objectproperty(object_id(specific_name), 'IsSchemaBound')
when 1 then 'Yes' else 'No' end as IsSchemaBnd,
case objectproperty(object_id(specific_name), 'IsDeterministic')
when 1 then 'Yes' else 'No' end as IsDtrmnstc
from information_Schema.routines
where routine_type = 'FUNCTION'
order by specific_name
go
schema function IsScalar IsTable IsInline IsSchemaBnd IsDtrmnstc
---------- -------------------- -------- ------- -------- ----------- ----------
dbo AverageBookPrice Yes No No No No
dbo AverageBookPrice2 Yes No No No No
dbo AveragePricebyType No Yes Yes No No
dbo AveragePricebyType2 No Yes No Yes Yes
dbo getdateonly Yes No No No No
dbo getonlydate Yes No No No No
dbo striptime Yes No No No No
dbo striptime2 Yes No No No No