查询的错误输出

akh*_*tty -2 postgresql insert dynamic-sql

使用以下查询时,我无法从 bnrcno 列中获取数据。

select 'insert into table values('||id||','||bnrcno||','||chargetype||','||domno||','||coa_id||','||liability_gl||','||revenue_gl||','||taxno||')' 
from bill_charges_map where  domno=5
Run Code Online (Sandbox Code Playgroud)

brcno列是整数类型,其中的大多数值都为空。

a_h*_*ame 7

使用format()带有占位符的函数来表示文字:%L

select format('insert into table values(%s, %L, %L, %s, %s, %L, %L, %L)', 
               id, bnrcno, chargetype, domno, coa_id, liability_gl, revenue_gl, taxno) 
from bill_charges_map 
where domno=5;
Run Code Online (Sandbox Code Playgroud)

对于数字列(integernumericfloat等)使用%s的占位符。对于所有其他类型,%L用作占位符以正确引用它们。


请注意,明确列出目标表中的列是一种良好的编码习惯: insert into the_table (col1, col2, col3) values (...)