Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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 如何在postgres触发器中复制bytea?_Postgresql - Fatal编程技术网

Postgresql 如何在postgres触发器中复制bytea?

Postgresql 如何在postgres触发器中复制bytea?,postgresql,Postgresql,谁能帮我在postgres触发器中从另一个表插入bytea CREATE OR REPLACE FUNCTION public.image_insert_trigger() RETURNS trigger AS $BODY$DECLARE dyn_sql text; tbname text; img text; BEGIN img:=NEW.img; tbname:='t'||left(NEW.code,4); dyn_sql:='INSERT INTO '||t

谁能帮我在postgres触发器中从另一个表插入bytea

CREATE OR REPLACE FUNCTION public.image_insert_trigger()
    RETURNS trigger AS
$BODY$DECLARE
    dyn_sql text;
    tbname text;
    img text;
BEGIN
img:=NEW.img;
tbname:='t'||left(NEW.code,4);
dyn_sql:='INSERT INTO '||tbname||' (id,code,img) VALUES ('||NEW.id||','||NEW.code||','||''''||img::bytea||''''||')';    
execute dyn_sql;
return NULL;
END;$BODY$ 
LANGUAGE plpgsql VOLATILE COST 100;
ALTER FUNCTION public.image_insert_trigger() OWNER TO postgres; 

不要使用字符串文字,请使用参数

begin
  tbname :='t'||left(NEW.code,4);
  dyn_sql := 'INSERT INTO '||quote_ident(tbname)||' (id,code,img) VALUES ($1, $2, $3)';
  execute dyn_sql 
     using new.id, new.code, new.img;
  return NULL;
END;

我更喜欢使用
format()
函数来定义动态SQL,因为它使实际SQL更易于阅读(至少对我来说是这样)


不要使用字符串文字,请使用参数

begin
  tbname :='t'||left(NEW.code,4);
  dyn_sql := 'INSERT INTO '||quote_ident(tbname)||' (id,code,img) VALUES ($1, $2, $3)';
  execute dyn_sql 
     using new.id, new.code, new.img;
  return NULL;
END;

我更喜欢使用
format()
函数来定义动态SQL,因为它使实际SQL更易于阅读(至少对我来说是这样)


传递非内联参数应该容易得多:

dyn_sql:='INSERT INTO '||tbname||' (id,code,img) VALUES ($1,$2,$3)';    
execute dyn_sql using NEW.id, NEW.code, img::bytea;

传递非内联参数应该容易得多:

dyn_sql:='INSERT INTO '||tbname||' (id,code,img) VALUES ($1,$2,$3)';    
execute dyn_sql using NEW.id, NEW.code, img::bytea;