我在将触发器中的bytea数据从一个表复制到另一个表时遇到问题。
原始查询如下:
INSERT INTO "mytable" ("dataid","data")
VALUES
(123456,'\x3008d1a10a000000050000000000000000000000fb27000009090504010804060a08020b020c040000000000000000000000000001000000'::bytea);
内部"插入"触发器,尝试将此值插入另一个表时出现语法错误。原因是在触发器内部值NEW.data已经被转义并且看起来像
0\010\321\241\012\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000\373\'000\000\011\011\005\004\001\010\004\006\012\010\002\013\002\014\004\000\000\000\000\000\000\000\000\000\000\000\000\000\001\000\000\000
有没有办法检索NEW.dataid实际值(在类型转换之前或作为字符串)?
P.S。我无法在生产数据库
上更改原始查询或将bytea_output转换或设置为十六进制触发功能代码:
BEGIN
PERFORM dblink('logsyellow', '
INSERT INTO datastorage(dataid,data)
VALUES(' || new.dataid || ','||new.data||')
');
RETURN new;
END;
我得到错误:
PL/pgSQL function fn_replicate_data() line 4 at PERFORM
2014-07-08 13:24:35 GMT OPERATOR: INSERT INTO mytable ( dataid, data ) VALUES ( 123456, '\x3008d1a10a000000050000000000000000000000fb27000009090504010804060a08020b020c040000000000000000000000000001000000'::bytea );
2014-07-08 13:24:35 GMT ERROR: syntax error (at near "\")
2014-07-08 13:24:35 GMT CONTEXT: Error occurred on dblink connection named "unnamed": could not execute command.
SQL-operator: "SELECT DBLINK_EXEC('logsyellow','INSERT INTO datastorage(dataid,data) VALUES('||new.dataid||','''||new.data||''') ')"
答案 0 :(得分:0)
最后我得到了一个解决方案..感谢所有人)
PERFORM dblink('logsyellow','INSERT INTO datastorage(dataid,data) VALUES(' || new.dataid || ','||quote_literal(new.data)||') ')