首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >postgres:函数查询中的动态条件

postgres:函数查询中的动态条件
EN

Stack Overflow用户
提问于 2016-09-20 05:25:05
回答 1查看 445关注 0票数 0

我有一个Postgres函数,我需要动态地添加一个条件参数,如果它被传递到这个函数中。下面是函数:

代码语言:javascript
复制
CREATE OR REPLACE FUNCTION public.get_appointments(
     for_business_id INTEGER,
     range_start DATE,
     range_end   DATE,
     for_staff_id INTEGER
    )
       RETURNS SETOF appointment
       LANGUAGE plpgsql STABLE
       AS $function$
     DECLARE
        appointment appointment;
        recurrence  TIMESTAMP;
        appointment_length INTERVAL;
        parent_id UUID;
     BEGIN
        FOR appointment IN
            -- NEED TO CONDITIONALLY ADD "WHERE staff_id = for_staff_id" 
            -- if for_staff_id is NOT NULL
            SELECT *
              FROM appointment
             WHERE business_id = for_business_id
               AND (
                       recurrence_pattern IS NOT NULL
                   OR  (
                          recurrence_pattern IS NULL
                      AND starts_at BETWEEN range_start AND range_end
                   )
               )
         LOOP
            IF appointment.recurrence_pattern IS NULL THEN
              RETURN NEXT appointment;
              CONTINUE;
            END IF;

            appointment_length := appointment.ends_at - appointment.starts_at;
            parent_id := appointment.id;

            FOR recurrence IN
                SELECT *
                  FROM generate_recurrences(
                           appointment.recurrence_pattern,
                           appointment.starts_at,
                           range_start,
                           range_end
                  )
            LOOP
                EXIT WHEN recurrence::date > range_end;
                CONTINUE WHEN (recurrence::date < range_start AND recurrence::date > range_end) OR recurrence = ANY(appointment.recurrence_exceptions);
                appointment.id := uuid_generate_v5(uuid_nil(), parent_id::varchar || recurrence::varchar);
                appointment.parent_id := parent_id;
                appointment.starts_at := recurrence;
                appointment.ends_at := recurrence + appointment_length;
                appointment.recurrence_pattern := appointment.recurrence_pattern;
                appointment.recurrence_exceptions := NULL;
                appointment.is_recurrence := true;
                RETURN NEXT appointment;
            END LOOP;
        END LOOP;
        RETURN;
     END;
    $function$;

如果将for_staff_id传递给函数,我需要将其作为条件(WHERE staff_id = for_staff_id)添加到位于FOR...IN循环中的查询中。我曾尝试在FOR...IN中执行IF/ELSE,但遇到语法错误。

有没有更好的方法来做这件事?

谢谢!

请注意,我运行的是Postgres 9.5。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-20 05:27:34

只需添加以下内容

代码语言:javascript
复制
... AND (for_staff_id IS NULL OR staff_id = for_staff_id)

或者这个

代码语言:javascript
复制
... AND coalesce(staff_id = for_staff_id, true)

SELECT语句的WHERE子句中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39582360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档