本文主要主要講解了SQL Server數據庫中函數的兩種用法
◆
函數部分
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
(FMERATE
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]
@ENID INT
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
AND ( ISNULL(MAXCOUNT
OR(ISNULL(@COUNT
SET @RATE=ISNULL(@QXS*@G
RETURN (@RATE)
END
調用函數的存儲過程部分:
CREATE PROC PROC_FAULTRATE
@PARTID INTEGER
@COUNT INTEGER
BEGIN
DECLARE
@TASKID INT
SET @RATE=
SELECT @TASKID=ISNULL(TASKPROID
WHERE ID=(SELECT PID FROM TABLERELATION WHERE ID=@ROID)
IF (@TASKID=
SET @RATE=
RETURN
END
SELECT @RATE=SUM([DBO]
(@PARTID
FROM TABLETASKPHASE
WHERE TASKID=@TASKID
END
GO
From:http://tw.wingwit.com/Article/program/SQLServer/201311/22423.html