基于多个条件的UDF查询中的返回值

时间:2014-07-22 12:08:23

标签: sql sql-server coldfusion user-defined-functions

大编辑以包含更多详情

我们为参加者展示会议供您选择。每个会议都有一个副本,被视为等待列表"版。等待列表ID存储在字段waitListProdID中的父记录中。某些字段也会被标记。

会议记录= isWaitlist = 0waitListProdID = *whatever its waitlist ID is* 会议候补名单记录= isWaitList = 1waitListProdID = 0

与会者仅在两个条件下查看等候名单版本:

  1. 当会议的currentAttendance等于maxAttendance
  2. 如果来自与会者公司的两个人已经在任何特定会议上注册并且当前的出席< maxAttendance
  3. 候补名单会议没有出席上限或公司限制。

    我的任务是生成一个UDF来处理第二个问题。它应该返回会议ID(主要或等候名单)。 UDF将接收3个输入和1个输出,其输出是传入的conferenceID或等效的等候名单。

    我知道我的UDF语法不正确,因为我对存储过程创建比较熟悉,但我尽量让它尽可能接近。

    //This query calls the UDF (build in ColdFusion)
    SELECT dt.conferenceID, ec.description, ec.price
    FROM (
       SELECT dbo.fnGetConfID(eci.conferenceID, #companyID#, #eventID#) as conferenceID
       FROM tblEventConferences eci
       WHERE eci.eventID = #eventID#
    ) dt
    INNER JOIN tblEventConferences eco ON eci.conferneceID = eco.conferenceID
    WHERE eco.eventID = #eventID#
    AND eco.currentAttendance < eco.maxAttendance
    
    //The function should return only the correct conferenceID
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE FUNCTION dbo.fnGetConfID (
        @conferenceID int,
        @companyID varchar(32),
        @eventID int,
    ) RETURNS int
    AS
    BEGIN
        DECLARE @count smallint;
        DECLARE @waitListID int;
        SET NOCOUNT ON;
    
        --Search only conferences that are not waitlist
        SELECT @count = count(rc.regID), @waitListID = rc.conferenceID
        FROM tblRegistration r
            INNER JOIN tblRegConferences rc ON r.ID = rc.regID
            INNER JOIN tblEventConferences ec ON rc.conferenceID = ec.conferenceID
        WHERE r.eventID = @eventID
        AND r.optfield2 = @companyID
        AND rc.conferenceID = @conferenceID
        AND ec.isWaitList = 0
        AND ec.currentAttendance < ec.maxAttendance
        GROUP BY rc.conferenceID
    
        --More than 1 person from same company registered for this conference. Return waitlistID
        IF @count > 1
        BEGIN
          SELECT @conferenceIDout = conferenceID
          FROM tblEventConferences
          WHERE conferenceID = @waitListID
        END
    
        --Company limit not reached. Return conferenceID that was used originally
        IF @count <= 1
        BEGIN
          SET @conferenceIDout = @conferenceID
        END
    
    END
    GO
    

0 个答案:

没有答案