函數部分:
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
SELECT @HOUR=ISNULL(TASKTIME,0) FROM TABLETASK
WHERE
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
SELECT @CA=SUM(ISNULL(Cvalue_M,0)) FROM TABLEFMECA WHERE
UPDATE TABLERELATION
SET
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 ) AND( ( (ISNULL(MINCOUNT,0)<=ISNULL(@COUNT,0)) AND ( ISNULL(MAXCOUNT,0)>=ISNULL(@COUNT,0)))
OR(ISNULL(@COUNT,0)>ISNULL(MAXCOUNT,0)))