select concat('insert into XX_TABLE(', string_agg(c.attname, ','),
') values ('
, string_agg(c.palce, ','),
')'
, ' on conflict (pk_name) do update set ( ',string_agg(c.attname, ','),')','=(',string_agg(c.f, ','),')') as sql
from (select 1 as fl, attname, '?' as palce, concat('EXCLUDED.',attname) as f
from pg_attribute
where attrelid = 'XX_TABLE'::regclass
and attnum > 0
order by attnum) c
group by c.fl
我这里写成了upsert, 不要的话,拿过去改改
- 里面的XX_TABLE 替换成表名
- pk_name 换成pk
- ‘XX_TABLE’::regclass 这里用到了类型转换,其实你也可以去pg_table里面关联查询
附加:
jdbc 插入字段为null, 报空指针异常解决
第一种:判断出属性为null, preparedStatement.setNull(index, param)
第二种: preparedStatement.setObject(index, param)

文章提供了一个用于构建PostgreSQLUPSERT语句的示例,该语句用于合并INSERT和UPDATE操作。同时,文章提到了在使用JDBC进行数据插入时遇到字段为NULL的情况,提供了两种解决方案:设置NULL值或使用setObject方法处理。
2903

被折叠的 条评论
为什么被折叠?



