国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

分析MS SQL Server里函數的兩種用法

2024-08-31 00:49:07
字體:
來源:轉載
供稿:網友

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 [email protected]
  select @hour=isnull(tasktime,0) from tabletask
  where [email protected]
  set @[email protected]*@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 [email protected] and taskphaseid=c.id and [email protected]
  select @ca=sum(isnull(cvalue_m,0)) from tablefmeca where [email protected]
  update tablerelation
  set [email protected]
  where [email protected]
  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 [email protected]
  select @g=isnull(frate_g,0) from tablefailurerate
  where ([email protected]) and( [email protected]) and ( [email protected]) 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 [email protected])
  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 [email protected]
  end
  go


  函數還可以返回表等,希望大家一起討論sqlserver里函數的妙用。

 

最大的網站源碼資源下載站,

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肥东县| 巴中市| 平凉市| 怀安县| 闽清县| 饶平县| 宁明县| 浦江县| 法库县| 辽中县| 高安市| 南投县| 三都| 白城市| 天等县| 木兰县| 连南| 丰台区| 西畴县| 边坝县| 巫溪县| 元朗区| 石河子市| 永宁县| 泰兴市| 彰化市| 扎鲁特旗| 宁津县| 宾阳县| 胶州市| 和顺县| 漾濞| 花莲市| 湖南省| 吕梁市| 西青区| 长垣县| 凤城市| 新源县| 弥勒县| 太原市|