postgresql - 在 postgresql 9.1 中插入或更新

标签 postgresql duplicates

我有一堆插入语句,它们各自的表有不同数量的列,如果记录不存在,我需要执行这些查询。我试图将其实现为

do $$
begin
IF not exists (SELECT 1 FROM gst_type_customer WHERE name = 'Unregistered') THEN
insert into gst_type_customer(create_date,write_date,create_uid,write_uid,name) values((now() at time zone 'UTC'),(now() at time zone 'UTC'),1,1,'Unregistered');
END IF;
end
$$

尽管上面的代码确实有效,但实现大量查询需要很多时间,所以我想制作一个我可以调用的存储过程

merge_check(insertquery,name[column to check for duplication],value)

但我无法直接执行插入查询。

到目前为止我想出了

CREATE OR REPLACE FUNCTION merge_tabla(data text)
RETURNS void AS
$BODY$
BEGIN
  execute(data);
END;
$BODY$
LANGUAGE plpgsql


select merge_table("insert into gst_type_customer(name) values('Unregistered')")

但是我得到一个错误提示

column "insert into gst_type_customer(name) values('Unregistered')" does not exist

最佳答案

您遇到的错误是调用函数时使用双引号引起的。这应该有效:

select merge_table(E'insert into gst_type_customer(name) values(\'Unregistered\')'::text)

您需要使用单引号(双引号用于列名,单引号用于字符串文字)并转义原始查询字符串中的任何单引号。

关于postgresql - 在 postgresql 9.1 中插入或更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44561296/

相关文章:

postgresql - 无法通过 Postgres 非空约束

delphi - postgreSQL 的开源组件

performance - PostgreSQL 统计问题 - 无法重命名临时统计文件

具有模式支持的 postgresql 副本

java - 检查ArrayList中是否有重复项。使用循环

MySQL多次插入: How to write a query to insert the new rows and ignore inserting duplicate rows?

IOS应用程序复制

java - Chat Application,如何存储聊天记录?

javascript - 使用随机生成的值检查数组中的重复项

php - 准备 SQL 语句并仅在不重复时输入