从不同的数据库插入表

时间:2011-05-25 03:03:33

标签: sql postgresql insert dblink

SQL

INSERT INTO t_publikasi
  ("idPublikasi","kdBahanPustaka","bulanTerbit", "tahunTerbit", "noUrutBI",
   "pdfDir", "judul")
SELECT * 
  FROM dblink('dbname=perpus2 port=5432 user=dbdev password=develop',
              'SELECT '113' || substring("no_pustaka",4,2) as bb ||
                      substring("no_pustaka",6,2) as cc ||
                      substring("no_pustaka",9,4) as dd,
                      '113', 
                      substring("no_pustaka",4,2) as bb,
                      substring("no_pustaka",6,2) as cc,
                      substring("no_pustaka",9,4) as dd,
                      "file_pdf","judul"
                 FROM kuesioner 
             ORDER BY bb, cc, dd') AS kuesioner (
    id varchar(10), 
    kd varchar(10),
    bln varchar(10),
    thn varchar(10),
    urut varchar(10),
    file_pdf varchar(50), 
    judul varchar(50));

错误

ERROR:  syntax error at or near "113"
LINE 1: ...2 port=5432 user=dbdev password=develop','SELECT '113' || su...

                                                            ^
ERROR: syntax error at or near "113"
SQL state: 42601

3 个答案:

答案 0 :(得分:2)

变化

'SELECT'113'

'SELECT 113

'SELECT“113”

希望它有所帮助。

答案 1 :(得分:0)

当你编写'SELECT '113'||时,你正在终止包含SELECT语句的dblink()调用的第二个参数,然后你启动一个113的数字文字。

'SELECT 113有效,因为它会选择一个数字而不是字符文字。 然后,||运算符将以静默方式将其转换回字符值。

在字符文字中嵌入单引号的正确方法是编写两个单引号:

, 'SELECT ''113''||substring(... '

答案 2 :(得分:0)

使用E或美元报价。

...
FROM dblink('dbname=perpus2 port=5432 user=dbdev password=develop',
          $my_dblink_arg$SELECT '113' ||
...
      FROM kuesioner 
         ORDER BY bb, cc, dd$my_dblink_arg$) AS kuesioner
...