You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PostgreSQL中使用DbLink执行插入操作?报错:列"Test"不存在

问题原因与解决方法

报错的核心是单引号嵌套冲突:你用单引号包裹了dblink的INSERT语句参数,但Values里的'Test'又用了单引号,PostgreSQL会误将Test识别为列名,导致"列'Test'不存在"的错误。

两种修复方案:

方案1:转义内部单引号

把内部字符串的单引号改成两个连续单引号,让PostgreSQL正确识别这是字符串值:

Select * 
from dblink('host=localhost user=postgres password=Test dbname=wb',
            'Insert Into tblProducts(AccountNumber,AccountProductNumber,supplierproductnumber)
             Values( 2012, 2022,''Test'') Returning ProductNumber'
     ) AS tblProducts(ProductNumber integer)

方案2:使用美元引号(推荐)

用$$包裹内部SQL语句,彻底避免单引号嵌套问题,内部单引号无需转义:

Select * 
from dblink('host=localhost user=postgres password=Test dbname=wb',
            $$Insert Into tblProducts(AccountNumber,AccountProductNumber,supplierproductnumber)
             Values( 2012, 2022,'Test') Returning ProductNumber$$
     ) AS tblProducts(ProductNumber integer)

内容的提问来源于stack exchange,提问作者Shashi Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 12:24:24