在deeplink中将jsonb转换为bigint时出现sql问题

2skhul33  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(342)

在相同的db中铸造时工作良好,但在使用deeplink时失败
在同一数据库中正常工作(通过此查询获得结果):

SELECT id, 
       total_inventory, 
       hotel_id, 
       ( room_type -> 'id' ) :: bigint AS room_type_id, 
       created_by, 
       created_date, 
       modified_by, 
       modified_date 
FROM   hotel_inventory;

与其他数据库连接时不工作(使用deeplink)(此查询出错):

INSERT INTO hotel_inventory 
            ( 
                        id, 
                        total_inventory, 
                        hotel_id, 
                        room_type_id, 
                        created_by, 
                        created_date, 
                        modified_by, 
                        modified_date 
            ) 
SELECT * 
FROM   dblink('demopostgres', 'SELECT id, total_inventory, hotel_id, (room_type -> 'id')::bigint as room_type_id,  created_by, created_date, modified_by, modified_date FROM hotel_inventory') 
AS data(id bigint, total_inventory integer, hotel_id bigint, room_type_id bigint, created_by jsonb, created_date timestamp without time zone, modified_by jsonb, modified_date timestamp without time zone);

错误:

ERROR:  syntax error at or near "id"
LINE 3: ...ECT id, total_inventory, hotel_id, (room_type -> 'id')::bigi...
                                                             ^
SQL state: 42601
Character: 221
gkl3eglg

gkl3eglg1#

您需要将单引号加倍以避免此类错误:

dblink('demopostgres',
       'SELECT . . . (room_type -> ''id'')::bigint as room_type_id . . . '
      )

问题是一个简单的解析错误。单引号结束字符串——因此出现错误。双单引号是将单引号放入字符串中的标准方法,尽管不同的数据库通常支持其他方法(例如反斜杠)。

相关问题