选择显示字体大小

sql server里函数的两种用法(可以代替游标)

sql server里函数的两种用法(可以代替游标)
1. 因为update里不能用存储过程,然而要根据更新表的某些字段还要进行计算。我们常常采用游标的方法,这里用函数的方法实现。

函数部分:
create function [dbo].[fun_gettime] (@taskphaseid int)
returns float as
begin
  declare @taskid int,
          @hour float,
          @percent float,
          @return float
  if @taskphaseid is null
  begin
    return(0.0)
  end

select @taskid=taskid,@percent=isnull(workpercent,0)/100
from tabletaskphase
where id=@taskphaseid

select @hour=isnull(tasktime,0) from tabletask
where id=@taskid

set @return=@hour*@percent
return (@return)
end

调用函数的存储过程部分
create procedure [dbo].[proc_calcca]
@roid int
  as
begin
  declare @ca float

  update tablefmeca
  set
  cvalue_m=    isnull(moderate,0)*isnull(fmerate,0)*isnull(b.basfailurerate,0)*[dbo].[fun_gettime](c.id)
from tablefmeca ,tablerelation b,tabletaskphase c
where roid=@roid and taskphaseid=c.id and b.id=@roid

  select @ca=sum(isnull(cvalue_m,0)) from tablefmeca where roid=@roid

update tablerelation
  set criticality=@ca
  where id=@roid
end
go

2. 我们要根据某表的某些记录,先计算后求和,因为无法存储中间值,平时我们也用游标的方法进行计算。但sqlserver2000里支持
sum ( [ all distinct ] expression )

expression

是常量、列或函数,或者是算术、按位与字符串等运算符的任意组合。因此我们可以利用这一功能。

函数部分:

create function [dbo].[fun_rate] (@partid int,@enid int,@sourceid int, @qualityid int,@count int)

returns float as
begin
  declare @qxs float, @g float, @rate float

  if (@enid=null) or (@partid=null) or (@sourceid=null) or (@qualityid=null)
  begin
    return(0.0)
  end

  select @qxs= isnull(xs,0) from tablequality where id=@qualityid
  select @g=isnull(frate_g,0) from tablefailurerate
  where (subkindid=@partid) and( enid=@enid) and ( datasourceid=@sourceid) and( ( (isnull(mincount,0)<=isnull(@count,0)) and ( isnull(maxcount,0)>=isnull(@count,0)))
or(isnull(@count,0)>isnull(maxcount,0)))

  set @rate=isnull(@qxs*@g,0)
  return (@rate)
end

调用函数的存储过程部分:

create proc proc_faultrate

@partid integer, @qualityid integer, @sourceid integer, @count integer, @roid int, @grade int,@rate float=0 outputas
begin
  declare
    @taskid int
    set @rate=0.0

select @taskid=isnull(taskproid,-1) from tablerelation where id=(select pid from tablerelation where id=@roid)

    if (@taskid=-1) or(@grade=1) begin
       set @rate=0
    return
end

    select @rate=sum([dbo].[fun_rate] (@partid,enid,@sourceid, @qualityid,@count) *isnull(workpercent,0)/100.0)

    from tabletaskphase
    where taskid=@taskid
end
go

函数还可以返回表等,希望大家一起讨论sqlserver里函数的妙用。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons