一、函数创建
SQL SERVER:
“自定义函数”是我们平常的说法,而“用户定义的函数”是 SQL Server 中书面的说法。
SQL Server 2000 允许用户创建自定义函数,自定义函数可以有返回值。
自定义函数分为:标量值函数或表值函数
如果 RETURNS 子句指定一种标量数据类型,则函数为标量值函数。可以使用多条 Transact-SQL 语句定义标量值函数。
如果 RETURNS 子句指定 TABLE,则函数为表值函数。
表值函数又可分为:内嵌表值函数(行内函数)或多语句函数
如果 RETURNS 子句指定的 TABLE 不附带列的列表,则该函数为内嵌表值函数。
如果 RETURNS 子句指定的 TABLE 类型带有列及其数据类型,则该函数是多语句表值函数。
1.标量值函数示例
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
2.内嵌表值函数示例
CREATE FUNCTION dbo.Foo()
RETURNS TABLE
AS
return select id, title from msgs
--内嵌表值函数只有一个 select 语句。
3.多语句表值函数示例(部分)
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 部分。
多语句函数及
标量值函数的主体中允许使用以下语句。未在下面的列表中列出的语句不能用在函数主体中。
1).赋值语句。
2).控制流语句。
3).DECLARE 语句,该语句定义函数局部的数据变量和游标。
4).SELECT 语句,该语句包含带有表达式的选择列表,其中的表达式将值赋予函数的局部变量。
5).游标操作,该操作引用在函数中声明、打开、关闭和释放的局部游标。只允许使用以 INTO 子句向局部变量赋值的 FETCH 语句;不允许使用将数据返回到客户端的 FETCH 语句。
6).INSERT、UPDATE 和 DELETE 语句,这些语句修改函数的局部 table 变量。
7).EXECUTE 语句调用扩展存储过程。
分享到:
相关推荐
oracle数据库函数大全集合,包含全部常用函数!
DB2数据库函数大全: DB2 函数简单解释 初学者适用~~~
MySql数据库函数大全[收集].pdf
Excel 340例常用函数说明,Ex常用函数,信息函数、数学与三角函数、数据库函数、文本函数、查找与引用函数、统计函数 信息函数18例、工程函数39例、数学与三角函数58例、数据库函数13例、文本函数34例、日期与时间...
数据库函数数据库函数
我自己整理的,认为还可以让大家看看,包括数据库和函数及查询语句
一个关于数据库函数的ppt文档,帮助有兴趣的人学习mysql数据库函数。
包含了oracle所有的函数 例如replace to_char date等67个函数
此包中含Excel所有函数的讲解与实例解析,对初学者很有帮助...
关于IBM cognos软件的相关数据库函数的学习资料
oracle数据库函数
DB2-SQL 数据库函数 DB2-SQL 数据库函数
MySQL数据库函数.docxMySQL数据库函数.docxMySQL数据库函数.docxMySQL数据库函数.docxMySQL数据库函数.docxMySQL数据库函数.docxMySQL数据库函数.docxMySQL数据库函数.docxMySQL数据库函数.docx
MySQL数据库函数.pdfMySQL数据库函数.pdfMySQL数据库函数.pdfMySQL数据库函数.pdfMySQL数据库函数.pdfMySQL数据库函数.pdfMySQL数据库函数.pdfMySQL数据库函数.pdf
收集了sql较为常用的函数语句: In:等值连接,用来查找多表相同字段的记录 Not In:非等值连接,用来查找不存在的记录 Inner join:内连接,主要用来查找都符合条件的记录 Left join:左连接,主要用来查找左边有,...
笔者找到的功能最强大、操作最简单、使用最可靠的数据库解密工具,用于解密的SQL Server2000、2005等版本的存储过程、函数、触发器和视图。 执行连接到数据库后,左边直接列出数据库清单以及每个数据库内的存储过程...
MySQL数据库函数,触发器,存储过程的视频课程,通俗易懂
数据库函数整理,包括db2和mysql的函数使用。
函数应用之数据库函数-secret.docx
PHP访问MySQL数据库函数简介 PHP访问MySQL数据库函数简介