Function 来自Newbee的Postgres函数

Function 来自Newbee的Postgres函数,function,postgresql,loops,Function,Postgresql,Loops,以下函数将在运行时给出错误 我注意到当我删除这一行和t.image\u id=mviews.image\u id 它起作用了 即使我将mviews.image_id更改为常量,它也可以工作 我认为我没有正确引用循环参数 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000

以下函数将在运行时给出错误

我注意到当我删除这一行
和t.image\u id=mviews.image\u id
它起作用了

即使我将mviews.image_id更改为常量,它也可以工作

我认为我没有正确引用循环参数

000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

CREATE OR REPLACE FUNCTION test() RETURNS void AS $$
  DECLARE
    mviews RECORD;     
BEGIN        
FOR mviews IN 
SELECT image_id FROM image_index_1205 WHERE image_type = '01' LIMIT 3
LOOP
copy    (

    SELECT encode(decode(image, 'base64'), 'hex') 
    FROM image_index_1205 t
    WHERE image_type = '01'
    AND t.image_id =  mviews.image_id
    LIMIT 1
    ) 
TO
    '/tmp/test.hex';

END LOOP;   
END;
$$ LANGUAGE plpgsql;
********** Error **********

ERROR: there is no parameter $1
SQL state: 42P02
Context: SQL statement "copy ( SELECT encode(decode(image, 'base64'), 'hex') FROM image_index_1205 t WHERE image_type = '01' AND t.image_id =  $1  LIMIT 1 ) TO '/tmp/test.hex'"
PL/pgSQL function "test" line 7 at SQL statement
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

CREATE OR REPLACE FUNCTION test() RETURNS void AS $$
  DECLARE
    mviews RECORD;     
BEGIN        
FOR mviews IN 
SELECT image_id FROM image_index_1205 WHERE image_type = '01' LIMIT 3
LOOP
copy    (

    SELECT encode(decode(image, 'base64'), 'hex') 
    FROM image_index_1205 t
    WHERE image_type = '01'
    AND t.image_id =  mviews.image_id
    LIMIT 1
    ) 
TO
    '/tmp/test.hex';

END LOOP;   
END;
$$ LANGUAGE plpgsql;
********** Error **********

ERROR: there is no parameter $1
SQL state: 42P02
Context: SQL statement "copy ( SELECT encode(decode(image, 'base64'), 'hex') FROM image_index_1205 t WHERE image_type = '01' AND t.image_id =  $1  LIMIT 1 ) TO '/tmp/test.hex'"
PL/pgSQL function "test" line 7 at SQL statement
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000。变量
mviews
COPY
的查询中不可见。您需要动态SQL。构建查询字符串并使用。像这样:

CREATE OR REPLACE FUNCTION test() RETURNS void AS $$
  DECLARE
    mviews RECORD;     
BEGIN        
FOR mviews IN 
SELECT image_id FROM image_index_1205 WHERE image_type = '01' LIMIT 3
LOOP
copy    (

    SELECT encode(decode(image, 'base64'), 'hex') 
    FROM image_index_1205 t
    WHERE image_type = '01'
    AND t.image_id =  mviews.image_id
    LIMIT 1
    ) 
TO
    '/tmp/test.hex';

END LOOP;   
END;
$$ LANGUAGE plpgsql;
********** Error **********

ERROR: there is no parameter $1
SQL state: 42P02
Context: SQL statement "copy ( SELECT encode(decode(image, 'base64'), 'hex') FROM image_index_1205 t WHERE image_type = '01' AND t.image_id =  $1  LIMIT 1 ) TO '/tmp/test.hex'"
PL/pgSQL function "test" line 7 at SQL statement
CREATE OR REPLACE FUNCTION test()
  RETURNS void AS
$BODY$
DECLARE
    mviews RECORD;     
BEGIN        

FOR mviews IN 
    SELECT image_id FROM image_index_1205 WHERE image_type = '01' LIMIT 3
LOOP
    EXECUTE $x$
    COPY (
       SELECT encode(decode(image, 'base64'), 'hex') 
       FROM   image_index_1205 t
       WHERE  image_type = '01'
       AND    t.image_id =  $x$ || mviews.image_id || $y$
       LIMIT 1
       ) 
    TO '/tmp/test.hex'
    $y$;
END LOOP;   

END;
$BODY$ LANGUAGE plpgsql;
我大量使用了,以简化报价处理

如果还修改了目标文件,那么该函数将更有意义。按照这种方式,每次迭代都会覆盖之前的一次,只有最后一次循环的结果才会持久。动态文件名还需要动态SQL