使用postgresql将查询结果存储在变量中返回错误的答案

时间:2016-08-07 10:19:07

标签: postgresql

我遇到了一个奇怪的问题,我使用的是postgresql,我希望将查询结果存储到变量中。这是我的功能代码

    select "GRIDS"."IDcluster" into clusterIDrow1 from "GRIDS" where "IDraster"=arow."IDraster";
RAISE DEBUG 'arow.IDraster is %',arow."IDraster";
    RAISE DEBUG 'clusterIDrow1 is %',clusterIDrow1;

这给了我

DEBUG:  arow.IDraster is 1
DEBUG:  clusterIDrow1 is 0

但必须返回clusterIDrow1 =44,因为此查询返回44

select "GRIDS"."IDcluster" from "GRIDS" where "IDraster"=1;
你可以帮我找出我的错误吗? 这是我的全部功能

CREATE OR REPLACE FUNCTION public.similarity_cal(c double precision,t double precision)
  RETURNS text AS
$func$
DECLARE
   num integer=1;
   algabra raster;
   width integer;
   height integer;
   sumOfsimilarity integer=1;
   arow record;
   secrow record;
   clusterIDrow1 double precision;
   clusterIDrow2 integer;
   result text;
BEGIN
set client_min_messages to 'debug';
UPDATE public."GRIDS"   SET "IDcluster"=0;

   FOR arow IN
      SELECT *
      FROM   "rasters"

   LOOP
           FOR secrow IN
              SELECT *
              FROM   "rasters"

           LOOP
            SELECT ST_width(arow.rast),ST_height(arow.rast) into width,height As pvc  ;

            RAISE DEBUG 'first loop';
            RAISE DEBUG ' width %', width;
            RAISE DEBUG ' height %', height; 

            CONTINUE WHEN arow.rid=secrow.rid;

            RAISE DEBUG 'start run algabra';
            SELECT
            ST_MapAlgebra(
                arow.rast, 1,
                secrow.rast, 1,
                '(abs([rast2.val] - [rast1.val]))'
            ) into algabra AS algabraTable;

          RAISE DEBUG 'end run algabra';
          RAISE DEBUG 'start run count';
            SELECT  sum((pvc).count) into sumOfsimilarity
            FROM (SELECT ST_ValueCount(algabra) As pvc) As foo where (pvc).value<c;
          RAISE DEBUG 'end run count, value is %',sumOfsimilarity;
          RAISE DEBUG 'Similarity condition is %',width*height*t;
                IF (sumOfsimilarity>=width*height*t) THEN
                    RAISE DEBUG 'similarity is true';

                    select "GRIDS"."IDcluster" into clusterIDrow1 from "GRIDS" where "IDraster"=arow."IDraster";

                    RAISE DEBUG 'arow.IDraster is %',arow."IDraster";
                    RAISE DEBUG 'clusterIDrow1 is %',clusterIDrow1;

                     IF (clusterIDrow1<>0) THEN
                        select "IDcluster" into clusterIDrow2 from "GRIDS" where "IDraster"=secrow."IDraster";
                        RAISE DEBUG 'clusterIDrow2 is %',clusterIDrow2;
                            IF (clusterIDrow2=0) THEN

                            RAISE DEBUG 'update grids  %',secrow."IDraster";

                                UPDATE public."GRIDS"
                                   SET "IDcluster"=clusterIDrow1  WHERE "IDraster"=secrow."IDraster";
                            END IF;
                    ELSE 
                        UPDATE public."GRIDS"
                           SET "IDcluster"=num  WHERE "IDraster"=arow."IDraster";
                        num=num+1;
                    END IF;
                END IF;



           END LOOP;


   END LOOP;

END
$func$  LANGUAGE plpgsql;

0 个答案:

没有答案