SQL Server裡函數的兩種用法(可以代替遊標)

來源:互聯網
上載者:User

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 ID=@TASKPHASEID

SELECT @HOUR=ISNULL(TASKTIME,0) FROM TABLETASK
WHERE ID=@TASKID

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 ROID=@ROID AND TASKPHASEID=C.ID AND B.ID=@ROID

  SELECT @CA=SUM(ISNULL(Cvalue_M,0)) FROM TABLEFMECA WHERE ROID=@ROID

UPDATE TABLERELATION
  SET CRITICALITY=@CA
  WHERE ID=@ROID
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 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

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.