如何在将结果插入HSQLDB表时对结果进行排序?

时间:2011-11-17 04:56:03

标签: sql hsqldb

当我使用HSQLDB运行以下脚本时,收到错误消息user lacks privilege or object not found: T2C1。如何在将行插入表TABLE3时对其进行排序?我可以在不添加其他表的情况下实现这一目标吗?

请注意,如果我对该行ORDER BY "T2C1", "T1C2"发表评论,我将不会收到任何错误消息,但TABLE3也不会被排序。

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

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

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

INSERT INTO "PUBLIC"."TABLE3" (
  "C1",
  "C2"
)
SELECT 
(SELECT
"T2"."C1" AS "T2C1"
FROM "PUBLIC"."TABLE2" "T2"
WHERE "T2"."C1" = "T1"."C1"),
"T1"."C2" AS "T1C2"
FROM "PUBLIC"."TABLE1" "T1"
ORDER BY "T2C1", "T1C2"
;

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

\x "PUBLIC"."TABLE3"

2 个答案:

答案 0 :(得分:0)

这对我来说没有多大意义,因为插入的顺序与其他论坛已经提到的其他论坛无关:

我认为HSQLDB只是给出了错误的错误信息。它不适用于MSSQL。

答案 1 :(得分:0)

HSQLDB 2.2.x支持在此处使用ORDER BY。 SELECT的问题是标量子查询缺少列别名T2C1。试试这个:

INSERT INTO "PUBLIC"."TABLE3" (
  "C1",
  "C2"
)
SELECT 
(SELECT
"T2"."C1" AS "T2C1"
FROM "PUBLIC"."TABLE2" "T2"
WHERE "T2"."C1" = "T1"."C1") AS T2C1,
"T1"."C2" AS "T1C2"
FROM "PUBLIC"."TABLE1" "T1"
ORDER BY "T2C1", "T1C2"
;