从不同数据库插入表

发布于 2024-11-09 16:49:18 字数 1174 浏览 4 评论 0原文

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

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));

ERRORS

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难得心□动 2024-11-16 16:49:18

更改

将'SELECT '113'

'SELECT 113

'SELECT "113"

希望有帮助。

change

'SELECT '113'

to

'SELECT 113

or

'SELECT "113"

Hope it helps.

热鲨 2024-11-16 16:49:18

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

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

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

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

When you write 'SELECT '113'|| you are terminating the second parameter for the dblink() call that contains the SELECT statement and then you start a numeric literal that is 113.

'SELECT 113 works because it will select a number instead of a character literal.
The || operator will then silently cast that back to a character value.

The proper way to embedd a single quote in a character literal is to write two single quotes:

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

伊面 2024-11-16 16:49:18

使用 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
...

Use E or dollar quoting.

...
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
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文