标签:
1, 通过 sys.sql_modules 和 sys.objects 的字段 object_id 关联,查询udf的metadata
SELECT o.object_id, o.name, o.type,o.type_desc, m.definition,m.is_schema_bound FROM sys.sql_modules AS m Inner JOIN sys.objects AS o ON m.object_id = o.object_id where o.type IN (‘FN‘, ‘IF‘, ‘TF‘) GO
确定object的类型字段:type
FN = SQL scalar function
IF = SQL inline table-valued function
TF = SQL table-valued-function
sys.sql_modules: Returns a row for each object that is an SQL language-defined module in SQL Server.
sys.objects: Contains a row for each user-defined, schema-scoped object that is created within a database.
sys.sql_modules 不仅包括udf的definition信息,还能查看用户定义的usp,view等的definition。查看MSDN sys.sql_modules
2,设置 Schemabinding option 创建强依赖关系
Schemabinding option 用于view 和udf。在创建 view 和udf 时,view 和udf 会引用一些underlying objects,Schemabinding option 用于将 underlying objects的 schema和 view 与 udf 绑定到一起,在view 和 udf中引用的column不能被删除或修改,也不能删除underlying objects。
示例
CREATE TABLE [dbo].[dt_study] ( [id] [int] NOT NULL PRIMARY KEY CLUSTERED , [name] [nvarchar](50) NULL, [sex] [bit] NULL, ) GO create view dbo.View_dt_Study_Male with schemabinding as select id,name,sex from dbo.dt_study where sex=0 go -- can not alter the underlying object colun because the view set schemabinding option alter table [dbo].[dt_study] alter column sex bit not null go
Msg 5074, Level 16, State 1, Line 1
The object ‘View_dt_Study_Male‘ is dependent on column ‘sex‘.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN sex failed because one or more objects access this column.
如果不使用 schemabinding option,underlying object被允许修改schema,以及被删除。当试图查询view或udf时,如果underlying objects不存在或 column不存在,就会导致runtime 错误。如果使用schemabinding option,就能避免这种修改被引用对象schema的错误发生。如果必须修改underlying objects,那么必须先修改 udf或view。
要支持schemabinding option,对象定义时必须满足两个requirement:
Best Practices
If a user-defined function is not created with the SCHEMABINDING clause, changes that are made to underlying objects can affect the definition of the function and produce unexpected results when it is invoked. We recommend that you implement one of the following methods to ensure that the function does not become outdated because of changes to its underlying objects:
Specify the WITH SCHEMABINDING clause when you are creating the function. This ensures that the objects referenced in the function definition cannot be modified unless the function is also modified.
Execute the sp_refreshsqlmodule stored procedure after modifying any object that is specified in the definition of the function.
参考文档:
https://msdn.microsoft.com/en-us/library/ms186755(v=sql.120).aspx
Metadata
The following table lists the system catalog views that you can use to return metadata about user-defined functions.
System View |
Description |
---|---|
Displays the definition of Transact-SQL user-defined functions. The definition of functions created by using the ENCRYPTION option cannot be viewed by using sys.sql_modules; however, other information about the encrypted functions is displayed. |
|
Displays information about CLR user-defined functions. |
|
Displays information about the parameters defined in user-defined functions. |
|
Displays the underlying objects referenced by a function. |
Schemabinding option and function definition
标签:
原文地址:http://www.cnblogs.com/ljhdo/p/5061297.html