`
xwood
  • 浏览: 104802 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

数据库函数

阅读更多
一、函数创建
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 语句调用扩展存储过程。
分享到:
评论

相关推荐

    数据库函数和查询语句

    综上所述,数据库函数和查询语句是数据库管理系统中的基础操作,用于创建、更新、删除和查询数据,同时还需要了解锁定机制和并发控制策略,以保证数据的准确性和完整性。掌握这些知识对于理解和操作数据库至关重要。

    DB2数据库函数大全

    这只是DB2数据库函数的一部分,实际上DB2还提供了许多其他功能强大的函数,包括日期处理、字符串操作、数学计算、类型转换等,它们在数据库查询和报表生成中起着至关重要的作用。掌握这些函数的使用,能极大地提高...

    MySql数据库函数大全[收集].pdf

    MySQL 数据库函数大全 MySQL 数据库函数大全是一份详细的函数大全,涵盖了字符串、数字、日期和时间等多个方面的函数。本文档将对其中的一些重要函数进行详细的解释。 一、字符串函数 ASCII(str) 函数返回字符串 ...

    UNIX环境高级编程-016_数据库函数库

    【UNIX环境高级编程-016_数据库函数库】章节主要介绍了在UNIX系统中如何设计和使用数据库函数库,特别是针对多用户环境的数据库访问。早期的UNIX系统由于缺乏有效的进程间通信(IPC)机制和记录锁,不适合构建多用户...

    cognos 数据库函数

    在IBM Cognos Analytics中,数据库函数是进行数据查询、分析和报告时不可或缺的一部分。Cognos使用SQL(结构化查询语言)与各种数据库进行交互,而SQL中的函数可以帮助我们处理和操纵数据。本篇文章将深入探讨Cognos...

    数据库函数数据库函数.doc

    数据库函数详解 数据库函数是数据库管理系统中的一种功能强大且实用的工具,它可以对字符串、数字、日期等数据类型进行处理和操作。本文将对常用的数据库函数进行详细的介绍,包括字符函数、字符串替代函数、字符级...

Global site tag (gtag.js) - Google Analytics