如果 RETURNS 子句指定一种标量数据类型,则函数为标量值函数。可以使用多条 Transact-sql 语句定义标量值函数。 如果 RETURNS 子句指定 TABLE,则函数为表值函数。 标量值函数: ALTER FUNCTION [dbo].[FnGetResult] ( @oldvalue int,--参数 @step int ) RETURNS int AS BEGIN -- Declare the return variable here DECLARE @result int -- Add the T-sql statements to compute the return value here SELECT @result=@oldvalue*@step --计算值 -- Return the result of the function RETURN @result --返回值 END 内联表值函数: ALTER FUNCTION [dbo].[GetRoleListByID] ( @RoleID int ) RETURNS TABLE AS RETURN ( -- Add the SELECT statement with parameter references here SELECT * FROM TRoleFunction WHERE RoleID=@RoleID ) 多语句表值函数: CREATE FUNCTION dbo.GetNewUserList ( @ID int ) RETURNS @tbNewUser TABLE ( ID int,Name nvarchar(20) ) AS BEGIN INSERT INTO @tbNewUser VALUES(1,'Ben.Jiang') INSERT INTO @tbNewUser VALUES(2,'Leo.Zheng' RETURN END GO
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。