sp_refreshsqlmodule (Transact-SQL)

新建日期: 2006 年 12 月 12 日

更新指定的未绑定到架构的存储过程、用户定义函数或视图的元数据。如果更改这些对象的基础对象,则这些对象的持久元数据(如参数的数据类型)将会过时。

主题链接图标Transact-SQL 语法约定

语法

 sp_refreshsqlmodule [ @name = ] 'module_name' 

参数

  • [ @name= ] 'module_name'
    为存储过程、函数或视图的名称。module_name 不能是公共语言运行时 (CLR) 存储过程或 CLR 函数。module_name 不能绑定到架构。module_name 的数据类型为 nvarchar,无默认值。module_name 可以是多部分组成的标识符,但只能引用当前数据库中的对象。

返回代码值

0(成功)或非零数字(失败)

备注

对影响存储过程、用户定义函数或视图的定义的基础对象进行更改时,应该运行 sp_refreshsqlmodule。否则,当查询或调用对象时,可能会生成意外结果。若要刷新视图,可以使用 sp_refreshsqlmodule 或具有相同效果的 sp_refreshview

sp_refreshsqlmodule 不会影响与对象关联的任何权限、扩展属性或 SET 选项。

运行 sp_refreshsqlmodule 时,将删除与对象关联的所有签名。

权限

要求对存储过程、函数或视图拥有 ALTER 权限,并对对象引用的 CLR 用户定义类型和 XML 架构集合拥有 REFERENCES 权限。

此外,对于使用 EXECUTE AS 子句定义的模块,要求对指定主体拥有 IMPERSONATE 权限。通常,刷新对象不会更改其 EXECUTE AS 主体,除非模块是使用 EXECUTE AS USER 定义的,并且主体的用户名现在解析为与创建模块时的用户不同的用户。

示例

以下示例刷新用户定义函数。此示例创建别名数据类型 mytype 和使用 mytype 的用户定义函数 to_upper。然后,mytype 将重命名为 myoldtype,并将创建具有不同定义的 mytype。刷新 to_upper 函数,以便它引用新实现的 mytype,而非其旧版本。

-- Create an alias type.
USE AdventureWorks;
GO
IF EXISTS (SELECT 'mytype' FROM sys.types WHERE name = 'mytype')
DROP TYPE mytype;
GO

CREATE TYPE mytype FROM nvarchar(5);
GO

IF OBJECT_ID ('to_upper', 'FN') IS NOT NULL
DROP FUNCTION to_upper;
GO

CREATE FUNCTION to_upper (@a mytype)
RETURNS mytype
WITH ENCRYPTION
AS
BEGIN
RETURN upper(@a)
END;
GO

SELECT dbo.to_upper('abcde');
GO

-- Increase the length of the alias type.
sp_rename 'mytype', 'myoldtype', 'userdatatype';
GO

CREATE TYPE mytype FROM nvarchar(10);
GO

-- The function parameter still uses the old type.
SELECT name, type_name(user_type_id) 
FROM sys.parameters 
WHERE object_id = OBJECT_ID('dbo.to_upper');
GO

SELECT dbo.to_upper('abcdefgh'); -- Fails because of truncation
GO

-- Refresh the function to bind to the renamed type.
EXEC sys.sp_refreshsqlmodule 'dbo.to_upper';

-- The function parameters are now bound to the correct type and the statement works correctly.
SELECT name, type_name(user_type_id) FROM sys.parameters
WHERE object_id = OBJECT_ID('dbo.to_upper');
GO

SELECT dbo.to_upper('abcdefgh');
GO

请参阅

参考

sp_refreshview (Transact-SQL)
数据库引擎存储过程 (Transact-SQL)

帮助和信息

获取 SQL Server 2005 帮助