java知识随笔整理-标量函数和表值函数

以sql server为例:

1、表值函数

用户定义表值函数返回 table 数据类型,表是单个 select 语句的结果集。

示例代码create function test_getemployeesalary

(
@employeeid varchar(20) –参数
)
returns table –返回类型为表
as
return
(
select * from dbo.temployee
where employeeid = @employeeid –通过一条sql查询语句获取表中数据
)

 –函数调用
select * from test_getemployeesalary(‘1’)
———————

2、标量值函数

函数很简单返回一个整型值,然后就可以在存储过程中调用了,不过调用的方式有所不同,象上面的表值函数调用是不需要所有者的,只要写函数名称就可以,对于标量值函数来说,是需要加上所有者的,比如所有者是dbo。

示例代码:
create function dbo.test_getmax
(
@a int, –函数的两个参数
@b int
)
returns int –返回类型为int
as
begin
declare @max int;
if(@a>=@b)
begin
set @max = @a;
end
else
begin
set @max = @b
end

return @max; –最后return返回@max中的值
end

–调用函数
select dbo.test_getmax(2,3);

注意:

(1)必须使用两部分组成函数的名字来调用函数,即所有者.对象名,如dbo.test_getmax(2,3)

(2)所有的传入参数前必须加@

(3)不要写漏和写错关键字,如as,returns,return

(4)returns后面不是跟一个变量,而是跟变量的返回值类型

(5)在begin/end语句块中,使用的是return

转自:https://blog.csdn.net/luming666/article/details/78532695

 

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐