使用 Excel 公式时的 PostgreSQL 转义字符

PostgreSQL escape characters when using Excel formula

我想生成这样的查询:

select id, '=FKERES(B2;'D:\nyunyuka\[kotyog.xlsx]Munka1'!$A:$B;2;0)' as
 excel_formula from table;

我了解到我必须使用 E 转义 ' 字符,但它似乎不起作用。我假设我也必须转义所有 \ 。但这对我来说变得越来越复杂。

我不确定我是否理解正确。希望我做到了。 您需要像这样添加额外的单引号:

select id, '=FKERES(B2;''D:\nyunyuka[kotyog.xlsx]Munka1''!$A:$B;2;0)' as excel_formula 
from table;

因此,如果 select 子句中的字符串中有单引号 ',则必须添加另一个 '' 才能使其生效。

除了将引号加倍外,您还可以使用 dollar quoting

    So use
    select id, $Q$=FKERES(B2;'D:\nyunyuka[kotyog.xlsx]Munka1'!$A:$B;2;0)$Q$ as excel_formula 
    from table;

    Instead of

    select id, '=FKERES(B2;''D:\nyunyuka[kotyog.xlsx]Munka1''!$A:$B;2;0)' as excel_formula 
    from table;

其中 $Q$ 代替了开盘价和收盘价 (')。对于几个实例通常不值得,但对于带有大量引号的长字符串,它肯定会派上用场。