Schemabinding option and function definition

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:

  1. 查询语句不允许在select 子句使用*,必须显式列出column name
  2. 在引用对象时,必须使用two_part 命名,即 schema_name.object_name

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


sys.sql_modules


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.


sys.assembly_modules


Displays information about CLR user-defined functions.


sys.parameters


Displays information about the parameters defined in user-defined functions.


sys.sql_expression_dependencies


Displays the underlying objects referenced by a function.

时间: 2024-07-31 19:02:47

Schemabinding option and function definition的相关文章

'init':member function definition looks like a ctor(构造函数的缩写)

具体编译的错误提示如下:warning C4183: 'init': member function definition looks like a ctor, but name does not match enclosing classwarning C4183: 'method': member function definition looks like a ctor, but name does not match enclosing classLinking...main.exe -

C lang:Definition function

Ax_note. in parameter for show_n_char() is formal parameter #include <stdio.h> #include <string.h> #define NAME "ENOMOTHEM, INC." #define ADDRESS "101 Beijing China" #define PLACE "Megapolis, CA 00000" #define WID

Callback Function

typedef void (*callbackFun)(int a, int b);struct exm { int type; callbackFun fun;}; A pointer is a special kind of variable that holds the address of another variable. The same concept applies to function pointers, except that instead of pointing to

Prompting with a Default Option 创建默认提示

Introduction This tutorial centers around various ways to prompt for user input with a default option available upon the user pressing Enter. By http://lee-mac.com/promptwithdefault.html Note 1: On Prompt String Format To achieve correct display of a

select option 转

//遍历option和添加.移除optionfunction changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] option").length if(shipping.value != "CA"){  $("select[@name=ISHIPTYPE] option").each(function(){   if($(this).val() == 111){   

Jquery操作select,左右移动,双击移动 取到所有option的值

$(function () { function MoveItem(fromId, toId) { $("#" + fromId + " option:selected").each(function () { $(this).appendTo($("#" + toId + ":not(:has(option[value=" + $(this).val() + "]))")); }); $("#&

全面理解Javascript中Function对象的属性和方法

函数是 JavaScript 中的基本数据类型,在函数这个对象上定义了一些属性和方法,下面我们逐一来介绍这些属性和方法,这对于理解Javascript的继承机制具有一定的帮助. 属性(Properties) arguments 获取当前正在执行的 Function 对象的所有参数,是一个类似数组但不是数组的对象,说它类似数组是因为其具有数组一样的访问性质及方式,可以由arguments[n]来访问对应的单个参数的值,并拥有数组长度属性length.还有就是arguments对象存储的是实际传递给

理解callback function in javascript

以下内容主要摘自[1,2] (1)In javascript, functions are first-class objects, which means functions can be used in a first-class manner like objects, since they are in fact objects themselves: They can be “stored in variables, passed as arguments to functions,

js 操作select和option

js 操作select和option 1.动态创建select function createSelect(){ var mySelect = document.createElement_x("select");          mySelect.id = "mySelect";           document.body.appendChild(mySelect);      } 2.添加选项option function addOption(){ //根