我需要在几个库/表中构建键的索引。对于单个字段索引已经多次这样做了,但是试图用多个字段创建基本相同的东西。
在下面的所有示例中,我缩写为显示所有方面,但使用更小的元素列表以使其更容易消化。如果我发出任何重命名错误,请道歉。
以下是在预定作业中每晚运行的工作查询的示例:
INSERT INTO LIBMAST.TABL_X1
(SELECT DISTINCT T1.FIELD1 FROM
(SELECT DISTINCT T1.FIELD1 FROM LIB1NAME.TABL T1
UNION ALL
SELECT DISTINCT T1.FIELD1 FROM LIB2NAME.TABL T1 ) T1
WHERE T1.FIELD1 NOT IN (SELECT T1.FIELD1 FROM LIBMAST.TABL_X1) ) ;
这是我的插入查询的测试版本,作为一个select语句来显示应该导入哪些记录。这很好,显示它应该是什么。
SELECT T2.* FROM
(SELECT DISTINCT 'PA' AS FIELD1, T1.NAME2 AS FIELD2, T1.NAME3 AS FIELD3, T1.NAME4 AS FIELD4, T1.NAME5L AS FIELD5 FROM
(SELECT DISTINCT T1.NAME2, T1.NAME3, T1.NAME4, T1.NAME5 FROM LIB1NAME.TABL T1
UNION ALL
SELECT DISTINCT T1.NAME2, T1.NAME3, T1.NAME4, T1.NAME5 FROM LIB2NAME.TABL T1 ) T1
) T2
WHERE (T2.FIELD1, T2.FIELD2, T2.FIELD3, T2.FIELD4, T2.FIELD5) NOT IN
(SELECT T3.FIELD1, T3.FIELD2, T3.FIELD3, T3.FIELD4, T3.FIELD5 FROM LIBMAST.TABLKEYS T3) ;
这是不起作用的insert语句。只有第一行代码发生了变化。
INSERT INTO LIBMAST.TABLKEYS
(SELECT DISTINCT 'PA' AS FIELD1, T1.NAME2 AS FIELD2, T1.NAME3 AS FIELD3, T1.NAME4 AS FIELD4, T1.NAME5L AS FIELD5 FROM
(SELECT DISTINCT T1.NAME2, T1.NAME3, T1.NAME4, T1.NAME5 FROM LIB1NAME.TABL T1
UNION ALL
SELECT DISTINCT T1.NAME2, T1.NAME3, T1.NAME4, T1.NAME5 FROM LIB2NAME.TABL T1 ) T1
) T2
WHERE (T2.FIELD1, T2.FIELD2, T2.FIELD3, T2.FIELD4, T2.FIELD5) NOT IN
(SELECT T3.FIELD1, T3.FIELD2, T3.FIELD3, T3.FIELD4, T3.FIELD5 FROM LIBMAST.TABLKEYS T3) ;
以下是我收到的错误消息的第一部分:
SQL状态:42601 供应商代码:-104 消息:[SQL0104]令牌T2无效。有效令牌:除了OFFSET以外,使用带有FETCH限制订单联盟的跳过等待。原因。 。 。 。 。 :在令牌T2处检测到语法错误。令牌T2不是有效令牌。有效令牌的部分列表是FOR USE SKIP WAIT WITH FETCH LIMIT ORDER UNION EXCEPT OFFSET。此列表假定语句在令牌
之前是正确的我没有看到语法有什么不同,但显然我错过了一些东西。
答案 0 :(得分:1)
您删除了SELECT
并将其替换为INSERT
....您需要离开SELECT
INSERT INTO LIBMAST.TABLKEYS
SELECT T2.* FROM
(SELECT DISTINCT 'PA' AS FIELD1, T1.NAME2 AS FIELD2, T1.NAME3 AS FIELD3, T1.NAME4 AS FIELD4, T1.NAME5L AS FIELD5 FROM
(SELECT DISTINCT T1.NAME2, T1.NAME3, T1.NAME4, T1.NAME5 FROM LIB1NAME.TABL T1
UNION ALL
SELECT DISTINCT T1.NAME2, T1.NAME3, T1.NAME4, T1.NAME5 FROM LIB2NAME.TABL T1 ) T1
) T2
WHERE (T2.FIELD1, T2.FIELD2, T2.FIELD3, T2.FIELD4, T2.FIELD5) NOT IN
(SELECT T3.FIELD1, T3.FIELD2, T3.FIELD3, T3.FIELD4, T3.FIELD5 FROM LIBMAST.TABLKEYS T3) ;
修改强>
对于第一个查询,请参考“我不需要额外的选择包装器”。
语句的语法始终为INSERT INTO table1 SELECT FROM <...>
简化的第一个陈述是:
INSERT INTO LIBMAST.TABL_X1
SELECT DISTINCT T1.FIELD1 FROM <...> T1
您的第二个选择简化为:
SELECT T2.* FROM <...> T2
这是您打算插入另一个表的结果集。因此:
INSERT INTO LIBMAST.TABLKEYS
SELECT T2.* FROM <...> T2
如果select只返回5列而TABLKEYS有10列,则需要指定5个列,如下所示:
INSERT INTO LIBMAST.TABLKEYS (field1, field2, field3, field4, field5)
SELECT T2.* FROM <...> T2