数据库函数

[size=xx-large][b]一、函数创建[/b][/size]
[size=large][b]SQL SERVER:[/b][/size]

“自定义函数”是我们平常的说法,而“用户定义的函数”是 SQL Server 中书面的说法。

SQL Server 2000 允许用户创建自定义函数,自定义函数可以有返回值。

自定义函数分为:标量值函数或表值函数

如果 RETURNS 子句指定一种标量数据类型,则函数为标量值函数。可以使用多条 Transact-SQL 语句定义标量值函数。
如果 RETURNS 子句指定 TABLE,则函数为表值函数。
表值函数又可分为:内嵌表值函数(行内函数)或多语句函数

如果 RETURNS 子句指定的 TABLE 不附带列的列表,则该函数为内嵌表值函数。
如果 RETURNS 子句指定的 TABLE 类型带有列及其数据类型,则该函数是多语句表值函数。

[b]1.标量值函数示例[/b]

create function getWorkerWelfare(@welfareid varchar(60), @absentsum varchar(10), @daymoney varchar(10), @type varchar(40))
returns decimal(7, 2)
as
begin
--declare @welfareid varchar(60)
--declare @absentsum varchar(10)
--declare @daymoney varchar(10)
--declare @type varchar(40)
--set @welfareid = 'EB2011021212973401863681954589888'
--set @absentsum = '0.5'
--set @daymoney = '80'
--set @type = '勤杂人员'

--创建补贴标准游标
declare cur_allo cursor for
select absentbegin, absentend, laborallowance, medicalallowance, attendallowance
from t_worker_allowance_standard
--where groupid = (select workerallowancegroupid from t_worker_welfare_standard where id = @welfareid)
where groupid = (select workerallowancegroupid from t_worker_welfare_standard where id = @welfareid)

--创建工人类型标准游标
declare cur_type cursor for
select monetizebegin, monetizeend, workertype, attendallowanceper
from t_worker_type_standard
--where groupid = (select workertypegroupid from t_worker_welfare_standard where id = @welfareid)
where groupid = (select workertypegroupid from t_worker_welfare_standard where id = @welfareid)

declare @absentbegin varchar(10)
declare @absentend varchar(10)
declare @laborallowance varchar(10)
declare @medicalallowance varchar(10)
declare @attendallowance varchar(10)

declare @monetizebegin varchar(10)
declare @monetizeend varchar(10)
declare @workertype varchar(40)
declare @attendallowanceper varchar(10)

declare @welfare decimal(7, 2)
set @welfare = 0

open cur_allo
open cur_type
fetch next from cur_allo into @absentbegin, @absentend, @laborallowance, @medicalallowance, @attendallowance
while @@fetch_status = 0
begin
if @@fetch_status = -2 continue
if @absentbegin = '#' set @absentbegin = '-1'
if @absentend = '#' set @absentend = '9999999999'
if cast(@absentsum as numeric(7, 2)) >= cast(@absentbegin as numeric(12, 2)) and cast(@absentsum as numeric(7,2)) < cast(@absentend as numeric(12, 2))
begin
--print('出勤区间:' + @absentbegin + '---' + @absentend);
fetch next from cur_type into @monetizebegin, @monetizeend, @workertype, @attendallowanceper
while @@fetch_status = 0
begin
if @monetizebegin = '#' set @monetizebegin = '-1'
if @monetizeend = '#' set @monetizeend = '9999999999'
if patindex('%' + @type + '%', @workertype) > 0 and cast(@daymoney as numeric(7, 2)) >= cast(@monetizebegin as numeric(12, 2)) and cast(@daymoney as numeric(7, 2)) < cast(@monetizeend as numeric(12, 2))
begin
--print('货币化工资区间:' + @monetizebegin + '-' + @monetizeend)
set @welfare = cast(@laborallowance as numeric(7, 2)) + cast(@medicalallowance as numeric(7, 2)) + cast(@attendallowance as numeric(7, 2)) * cast(@attendallowanceper as numeric(7,2)) / 100
--print('福利工资:' + cast(@welfare as varchar(20)))
end
--print('货币化区间:' + @monetizebegin + '---' + @monetizeend)
fetch next from cur_type into @monetizebegin, @monetizeend, @workertype, @attendallowanceper
end
end
fetch next from cur_allo into @absentbegin, @absentend, @laborallowance, @medicalallowance, @attendallowance
end
close cur_allo
close cur_type
deallocate cur_allo
deallocate cur_type

return @welfare
end


[b]2.内嵌表值函数示例[/b]

CREATE FUNCTION dbo.Foo()
RETURNS TABLE
AS
return select id, title from msgs
--内嵌表值函数只有一个 select 语句。


[b]3.多语句表值函数示例(部分)[/b]

CREATE FUNCTION fn_FindReports (@InEmpId nchar(5))
RETURNS @retFindReports TABLE (empid nchar(5) primary key,
empname nvarchar(50) NOT NULL,
mgrid nchar(5),
title nvarchar(30))
...
--注意其 RETURNS 部分。

[b]多语句函数[/b]及[b]标量值函数[/b]的主体中允许使用以下语句。未在下面的列表中列出的语句不能用在函数主体中。

1).赋值语句。
2).控制流语句。
3).DECLARE 语句,该语句定义函数局部的数据变量和游标。
4).SELECT 语句,该语句包含带有表达式的选择列表,其中的表达式将值赋予函数的局部变量。
5).游标操作,该操作引用在函数中声明、打开、关闭和释放的局部游标。只允许使用以 INTO 子句向局部变量赋值的 FETCH 语句;不允许使用将数据返回到客户端的 FETCH 语句。
6).INSERT、UPDATE 和 DELETE 语句,这些语句修改函数的局部 table 变量。
7).EXECUTE 语句调用扩展存储过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值