Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如何解决psql函数中的daterange错误?_Postgresql - Fatal编程技术网

Postgresql 如何解决psql函数中的daterange错误?

Postgresql 如何解决psql函数中的daterange错误?,postgresql,Postgresql,我有一个psql函数,如下所示: create or replace function close_issue(issue varchar(128), device bigint, issue_daterange daterange) returns integer as $$ declare curr_daterange daterange; begin if lower_inf(issue_daterange) then raise except

我有一个psql函数,如下所示:

create or replace function close_issue(issue varchar(128), device bigint, issue_daterange daterange) returns integer as $$
 declare curr_daterange daterange;

 begin
        if lower_inf(issue_daterange) then
            raise exception 'Issue date % cannot have lower bound infinity when closing issues', issue_daterange
                  USING HINT = 'issue_date should have a date range with interval, [date, date)';
       
        elsif upper_inf(issue_daterange) then
            raise exception 'Issue date % should not have upper bound infinity when closing issues', issue_daterange
                  USING HINT = 'issue_date should have a date range with interval, [date, date)';
        end if;
        
       if EXISTS(select * from issue_device_relation where issue_id = issue and device_id = device) then        
        select distinct on (occurrence_id) occurrence_id, issue_date into curr_daterange from issue_device_relation where issue_id = issue and 
            device_id = device order by occurrence_id desc;
        
            raise notice 'date range %', curr_daterange;
        
            if not (lower_inf(curr_daterange) and upper_inf(curr_daterange)) then --closed interval
                raise notice 'issue_id % on device_id % is already closed', issue, device;
                return 1;
        
            elsif not (issue_daterange <@ curr_daterange) then -- issue cannot be closed in history 
                raise notice 'issue_id % cannot be closed on device_id % with historical date range %', issue, device, issue_daterange;
                return 2;
        
            else
                raise notice 'issue is closed.'; --update date range here 
                return 3;
            end if;
      else
       raise notice 'issue id and device id not found';
       return 4;
      end if;
 end;
 $$
 LANGUAGE plpgsql SECURITY definer;
它给了我以下错误:

SQL错误[22P02]:错误:格式错误的范围文字:“1” 详细信息:缺少左括号或括号


我有一种预感,这可能是由于日期范围造成的,但无法确定到底是什么地方出了问题。

最好包含错误的行号。我怀疑它来自
选择distinct on(concurrence\u id)concurrence\u id,将日期发布到curr\u daterange
current_daterange
被声明为daterange,而
select into
正试图将
事件id
(猜测1)强制输入其中。即使两个值都是日期,
选择进入
也会失败。您需要将它们作为
daterange
传递到
current\u daterange
。我只需要将问题日期传递到curr\u daterange,而不是事件。首先,是
发行日期
a
日期范围
?如果没有,则需要将其设置为一个,然后才能将其分配到
curr\u daterange
。其次,
选择distinct on(occurrence\u id)occurrence\u id,将日期发布到curr\u daterange
将不起作用,因为您试图将两列分配给一个变量。有关更多信息,请参见issue_date是一个数据范围,我试图实现的是获取最大发生时间的issue_date。是的,似乎是将两列赋给一个变量。然后从返回的字段中去掉
occurrence\u id
。此外,您没有得到
max
出现次数。您将在(事件id)上获得不同的
。如果它们彼此不同,则可能会返回多个
事件\u id
s。
 select close_issue('test_issue', 12345, daterange('2020-09-01'::date, '2020-10-01'::date, '[)'))