如何在HSQLDB中使用多行SELECT语句导出数据?

时间:2011-11-17 15:40:13

标签: sql database hsqldb

当我使用命令“java -jar sqltool.jar --rcFile sqltool.rc db_file q.sql”在HSQLDB 2.2.5中运行以下脚本时,收到错误消息:“user缺少权限或找不到对象: 选择”。如果我使\x命令成为单行语句,HSQLDB将成功执行脚本。为了更好的可读性,我想将\x分成多行。如何在HSQLDB脚本中使用多行\x命令?

--q.sql
DROP TABLE "PUBLIC"."TABLE1" IF EXISTS;

CREATE TABLE "PUBLIC"."TABLE1" (
  "C1" VARCHAR(10),
  "C2" VARCHAR(10),
  "C3" VARCHAR(10)
);

INSERT INTO "PUBLIC"."TABLE1" ("C1", "C2", "C3") VALUES ('a', 'b', 'c');
INSERT INTO "PUBLIC"."TABLE1" ("C1", "C2", "C3") VALUES ('d', 'e', 'f');

DROP TABLE "PUBLIC"."TABLE2" IF EXISTS;

CREATE TABLE "PUBLIC"."TABLE2" (
  "C1" VARCHAR(10),
  "C2" VARCHAR(10)
);

INSERT INTO "PUBLIC"."TABLE2" ("C1", "C2") VALUES ('a', 'b');
INSERT INTO "PUBLIC"."TABLE2" ("C1", "C2") VALUES ('u', 'v');

* *DSV_COL_DELIM=,
* *DSV_ROW_DELIM=\n
* *DSV_TARGET_FILE=results.csv

\x SELECT
( SELECT TOP 1 "T2"."C1" FROM "PUBLIC"."TABLE2" "T2" WHERE "T2"."C1" = "T1"."C1") AS "T2C1",
"T1"."C2" AS "T1C2"
FROM "PUBLIC"."TABLE1" "T1"
ORDER BY "T2C1", "T1C2"

更新:根据Per @ Blaine的建议,我切换到HSQLDB 2.2.6并使用以下内容替换上述脚本中的\x命令:

\.

SELECT
(SELECT TOP 1 "T2"."C1" FROM "PUBLIC"."TABLE2" "T2"
 WHERE "T2"."C1" = "T1"."C1") AS "T2C1",
"T1"."C2" AS "T1C2"
FROM "PUBLIC"."TABLE1" "T1"
ORDER BY "T2C1", "T1C2"

.;

\x :

1 个答案:

答案 0 :(得分:2)

这需要2.2.6版本。

一旦你拥有2.2.6,\x?将向你展示你需要知道的一切。

由于您希望从脚本中使用它,您可以使用原始模式在SqlTool的命令缓冲区中输入多行SQL语句,然后使用\x :。助记符:“:”表示SqlTool的编辑缓冲区。