函數部分
CREATE FUNCTION [DBO]
RETURNS FLOAT AS
BEGIN
DECLARE @TASKID INT
@HOUR FLOAT
@PERCENT FLOAT
@RETURN FLOAT
IF @TASKPHASEID IS NULL
BEGIN
RETURN(
END
SELECT @TASKID=TASKID
FROM TABLETASKPHASE
WHERE ID=@TASKPHASEID
SELECT @HOUR=ISNULL(TASKTIME
WHERE ID=@TASKID
SET @RETURN=@HOUR*@PERCENT
RETURN (@RETURN)
END
調用函數的存儲過程部分
CREATE PROCEDURE [DBO]
@ROID INT
AS
BEGIN
DECLARE @CA FLOAT
UPDATE TABLEFMECA
SET
Cvalue_M= ISNULL(MODERATE
FROM TABLEFMECA
WHERE ROID=@ROID AND TASKPHASEID=C
SELECT @CA=SUM(ISNULL(Cvalue_M
UPDATE TABLERELATION
SET CRITICALITY=@CA
WHERE ID=@ROID
END
GO
SUM ( [ ALL | DISTINCT ] expression )
expression
是常量
函數部分
CREATE FUNCTION [DBO]
RETURNS FLOAT AS
BEGIN
DECLARE @QXS FLOAT
IF (@ENID=NULL) OR (@PARTID=NULL) OR (@SOURCEID=NULL) OR (@QUALITYID=NULL)
BEGIN
RETURN(
END
SELECT @QXS= ISNULL(XS
SELECT @G=ISNULL(FRATE_G
WHERE (SUBKINDID=@PARTID) AND( ENID=@ENID) AND ( DATASOURCEID=@SOURCEID) AND( ( (ISNULL(MINCOUNT
OR(ISNULL(@COUNT
SET @RATE=ISNULL(@QXS*@G
RETURN (@RATE)
END
調用函數的存儲過程部分
CREATE PROC PROC_FAULTRATE
@PARTID INTEGER
BEGIN
DECLARE
@TASKID INT
SET @RATE=
SELECT @TASKID=ISNULL(TASKPROID
IF (@TASKID=
SET @RATE=
RETURN
END
SELECT @RATE=SUM([DBO]
FROM TABLETASKPHASE
WHERE TASKID=@TASKID
END
GO
函數還可以返回表等
From:http://tw.wingwit.com/Article/program/SQLServer/201311/22538.html