我想知道如何有条件地使用函数参数。这是我的函数,您可以阅读查询中的注释:
CREATE OR REPLACE FUNCTION mediabase.select_media(sysEnvironment character varying, statusId integer)
RETURNS refcursor AS
$BODY$
DECLARE
ref refcursor;
BEGIN
OPEN ref FOR
SELECT media.id, media.title, media.unique_filename, media.owner_id, media.status_id, media.location_name_id, media.upload_user_id, media.upload_ip, media.metadata_id, media.type_id, media.description, media.system_environment, media.upload_date, media.gps_location, media.language_id, media_publications.publication_id, media.limitations, media_categories.category_id, metadata.width, metadata.height, metadata.equipment, metadata.copyright, metadata.creation_time, metadata.file_format, metadata.resolution, metadata.resolution_unit, metadata.gps_longitude, metadata.gps_latitude, metadata.artist, metadata.color_space, metadata.gps_altitude, metadata.software_used, metadata.user_comment
FROM mediabase.media, mediabase.metadata, mediabase.media_categories, mediabase.media_publications
WHERE media.metadata_id = metadata.id
AND media.id = media_categories.media_id
AND media.id = media_publications.media_id
-- Problem: this CASE doesn't work of course
CASE statusId <> -1 THEN
AND media.status_Id = statusId
END
-- End problem
AND media.system_environment = sysEnvironment
ORDER BY media.upload_date DESC;
RETURN ref;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
我只需要使用'statusId'参数,如果它不同于-1,否则我将不会收到任何结果,因为当然没有状态-1。稍后,我需要添加更多此类过滤器。