程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQL Server 中“函數”的兩種用法(2)

SQL Server 中“函數”的兩種用法(2)

編輯:關於SqlServer

   函數部分:
  
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
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved