INSERT上的INSERT失败但在明确提到列时运行正常

时间:2017-05-18 10:54:30

标签: sql insert teradata

我有两个相同的表,上面有1x1视图。两者具有相同的列和数据类型。

当我执行以下命令时,

INSERT INTO MAIN_DB.TABLE_A 
   SELECT * FROM TEMP_DB.TABLE_A;

失败并弹出以下错误

  

错误[HY000] [Teradata] [ODBC Teradata驱动程序] [Teradata数据库]   DateTime或Interval的操作无效。 INSERT命令失败。

然而,当我使用显式列名运行INSERT查询而不是*时 。它执行得很好。

这很好用 -

INSERT INTO MAIN_DB.TABLE_A 
   SELECT COL1, COL2, COL3..... COL85 FROM TEMP_DB.TABLE_A;

我无法确定点 -

  • 为什么会这样?
  • 如何查找哪个列无效 约会?

编辑 - 表列的顺序完全相同(和数据类型相同)。

  

SHOW TABLE MAIN_DB.TABLE_A

CREATE MULTISET TABLE MAIN_DB.TABLE_A ,FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      COL1 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      COL2 DATE FORMAT 'yyyy-mm-dd',
      COL3 VARCHAR(12) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL4 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL5 VARCHAR(12) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL6 INTEGER,
      COL7 CHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL8 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL9 VARCHAR(40) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL10 VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL11 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL12 CHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL13 CHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL14 TIMESTAMP(6),
      COL15 TIMESTAMP(6),
      COL16 TIMESTAMP(6),
      COL17 DATE FORMAT 'yyyy-mm-dd' COMPRESS ,
      COL18 BYTEINT COMPRESS (0 ,1 ),
      COL19 BYTEINT COMPRESS (0 ,1 ),
      COL20 BYTEINT COMPRESS (0 ,1 ),
      COL21 BYTEINT COMPRESS (0 ,1 ),
      COL22 BYTEINT COMPRESS (0 ,1 ),
      COL23 BYTEINT COMPRESS (0 ,1 ),
      COL24 BYTEINT COMPRESS (0 ,1 ),
      COL25 BYTEINT COMPRESS (0 ,1 ),
      COL26 BYTEINT COMPRESS (0 ,1 ),
      COL27 BYTEINT COMPRESS (0 ,1 ),
      COL28 BYTEINT COMPRESS (0 ,1 ),
      COL29 BYTEINT COMPRESS (0 ,1 ),
      COL30 BYTEINT COMPRESS (0 ,1 ),
      COL31 BYTEINT COMPRESS (0 ,1 ),
      COL32 BYTEINT COMPRESS (0 ,1 ),
      COL33 BYTEINT COMPRESS (0 ,1 ),
      COL34 BYTEINT COMPRESS (0 ,1 ),
      COL35 BYTEINT COMPRESS (0 ,1 ),
      COL36 BYTEINT COMPRESS (0 ,1 ),
      COL37 TIMESTAMP(6),
      COL38 SMALLINT COMPRESS (0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ),
      COL39 BYTEINT COMPRESS (0 ,1 ),
      COL40 TIMESTAMP(6),
      COL41 VARCHAR(100) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL42 DECIMAL(18,0) COMPRESS 0. ,
      COL43 INTEGER COMPRESS 0 ,
      COL44 INTEGER COMPRESS 0 ,
      COL45 INTEGER COMPRESS 0 ,
      COL46 INTEGER COMPRESS 0 ,
      COL47 INTEGER COMPRESS 0 ,
      COL48 INTEGER COMPRESS 0 ,
      COL49 INTEGER COMPRESS 0 ,
      COL50 CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC COMPRESS,
      COL51 VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL52 BYTEINT COMPRESS (0 ,1 ),
      COL53 BYTEINT COMPRESS (0 ,1 ),
      COL54 BYTEINT COMPRESS (0 ,1 ),
      COL55 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL56 BYTEINT COMPRESS (0 ,1 ),
      COL57 BYTEINT COMPRESS (0 ,1 ),
      COL58 BYTEINT COMPRESS (0 ,1 ),
      COL59 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL60 BYTEINT COMPRESS (0 ,1 ),
      COL61 BYTEINT COMPRESS (0 ,1 ),
      COL62 BYTEINT COMPRESS (0 ,1 ),
      COL63 BYTEINT COMPRESS (0 ,1 ),
      COL64 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL65 BYTEINT COMPRESS (0 ,1 ),
      COL66 BYTEINT COMPRESS (0 ,1 ),
      COL67 BYTEINT COMPRESS (0 ,1 ),
      COL68 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL69 TIMESTAMP(6),
      COL70 CHAR(2) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL71 DECIMAL(18,2) NOT NULL,
      COL72 INTEGER COMPRESS (60 ,61 ,62 ),
      COL73 TIMESTAMP(6) NOT NULL,
      COL74 TIMESTAMP(6) NOT NULL,
      COL75 BYTEINT COMPRESS (0 ,1 ),
      COL76 BYTEINT COMPRESS (0 ,1 ),
      COL77 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL78 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL79 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL80 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL81 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL82 VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL83 VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL84 VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,
      COL85 VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC)
PRIMARY INDEX NUPI_TABLE_A  ( COL1 )
PARTITION BY RANGE_N(COL2  BETWEEN '2009-01-01' AND '2025-01-29' EACH INTERVAL '1' DAY ,
 NO RANGE OR UNKNOWN);
  

SHOW TABLE TEMP_DB.TABLE_A

CREATE MULTISET TABLE TEMP_DB.TABLE_A ,FALLBACK ,
         NO BEFORE JOURNAL,
         NO AFTER JOURNAL,
         CHECKSUM = DEFAULT,
         DEFAULT MERGEBLOCKRATIO
         (
          COL1 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
          COL2 DATE FORMAT 'yyyy-mm-dd',
          COL3 VARCHAR(12) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL4 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL5 VARCHAR(12) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL6 INTEGER,
          COL7 CHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL8 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL9 VARCHAR(40) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL10 VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL11 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL12 CHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL13 CHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL14 TIMESTAMP(6),
          COL15 TIMESTAMP(6),
          COL16 TIMESTAMP(6),
          COL17 DATE FORMAT 'yyyy-mm-dd' COMPRESS ,
          COL18 BYTEINT COMPRESS (0 ,1 ),
          COL19 BYTEINT COMPRESS (0 ,1 ),
          COL20 BYTEINT COMPRESS (0 ,1 ),
          COL21 BYTEINT COMPRESS (0 ,1 ),
          COL22 BYTEINT COMPRESS (0 ,1 ),
          COL23 BYTEINT COMPRESS (0 ,1 ),
          COL24 BYTEINT COMPRESS (0 ,1 ),
          COL25 BYTEINT COMPRESS (0 ,1 ),
          COL26 BYTEINT COMPRESS (0 ,1 ),
          COL27 BYTEINT COMPRESS (0 ,1 ),
          COL28 BYTEINT COMPRESS (0 ,1 ),
          COL29 BYTEINT COMPRESS (0 ,1 ),
          COL30 BYTEINT COMPRESS (0 ,1 ),
          COL31 BYTEINT COMPRESS (0 ,1 ),
          COL32 BYTEINT COMPRESS (0 ,1 ),
          COL33 BYTEINT COMPRESS (0 ,1 ),
          COL34 BYTEINT COMPRESS (0 ,1 ),
          COL35 BYTEINT COMPRESS (0 ,1 ),
          COL36 BYTEINT COMPRESS (0 ,1 ),
          COL37 TIMESTAMP(6),
          COL38 SMALLINT COMPRESS (0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ),
          COL39 BYTEINT COMPRESS (0 ,1 ),
          COL40 TIMESTAMP(6),
          COL41 VARCHAR(100) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL42 DECIMAL(18,0) COMPRESS 0. ,
          COL43 INTEGER COMPRESS 0 ,
          COL44 INTEGER COMPRESS 0 ,
          COL45 INTEGER COMPRESS 0 ,
          COL46 INTEGER COMPRESS 0 ,
          COL47 INTEGER COMPRESS 0 ,
          COL48 INTEGER COMPRESS 0 ,
          COL49 INTEGER COMPRESS 0 ,
          COL50 CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC COMPRESS,
          COL51 VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL52 BYTEINT COMPRESS (0 ,1 ),
          COL53 BYTEINT COMPRESS (0 ,1 ),
          COL54 BYTEINT COMPRESS (0 ,1 ),
          COL55 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL56 BYTEINT COMPRESS (0 ,1 ),
          COL57 BYTEINT COMPRESS (0 ,1 ),
          COL58 BYTEINT COMPRESS (0 ,1 ),
          COL59 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL60 BYTEINT COMPRESS (0 ,1 ),
          COL61 BYTEINT COMPRESS (0 ,1 ),
          COL62 BYTEINT COMPRESS (0 ,1 ),
          COL63 BYTEINT COMPRESS (0 ,1 ),
          COL64 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL65 BYTEINT COMPRESS (0 ,1 ),
          COL66 BYTEINT COMPRESS (0 ,1 ),
          COL67 BYTEINT COMPRESS (0 ,1 ),
          COL68 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL69 TIMESTAMP(6),
          COL70 CHAR(2) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL71 DECIMAL(18,2) NOT NULL,
          COL72 INTEGER COMPRESS (60 ,61 ,62 ),
          COL73 TIMESTAMP(6) NOT NULL,
          COL74 TIMESTAMP(6) NOT NULL,
          COL75 BYTEINT COMPRESS (0 ,1 ),
          COL76 BYTEINT COMPRESS (0 ,1 ),
          COL77 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL78 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL79 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL80 VARCHAR(5) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL81 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL82 VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL83 VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL84 VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,
          COL85 VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC)
    PRIMARY INDEX NUPI_TABLE_A  ( COL1 )
    PARTITION BY RANGE_N(COL2  BETWEEN '2009-01-01' AND '2020-01-30' EACH INTERVAL '1' DAY ,
     NO RANGE OR UNKNOWN);

实际列名称更改为COL1,COL2 ......等等。

2 个答案:

答案 0 :(得分:1)

我可以猜出问题是什么。 MAIN_DB.TABLE_A中的列与TEMP_DB.TABLE_A中的列的顺序不同。当您使用*时,它们不会对齐。请记住,INSERT按列位置排列,而不是按列名称排列。

使用INSERT时,您应该始终列出INSERT和进入值的列:

INSERT INTO MAIN_DB.TABLE_A (COL1, COL2 . . . )
    SELECT COL1, COL2, COL3..... COL85
    FROM TEMP_DB.TABLE_A;

您可能会抱怨查询更长更复杂,但从两个表中获取列列表非常容易。然后代码更加健壮。

答案 1 :(得分:0)

我发现了什么问题。

桌子被改变并且还原了。然而,这个观点并没有改变。

例如

最初的表是

COL1 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
COL2 DATE FORMAT 'yyyy-mm-dd',
COL3 VARCHAR(12) CHARACTER SET LATIN NOT CASESPECIFIC

然后它被改为

COL1 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
COL2 DATE FORMAT 'yyyy-mm-dd',
COL3 VARCHAR(12) CHARACTER SET LATIN NOT CASESPECIFIC,
NEW_COLUMN VARCHAR(100)

并恢复到

COL1 VARCHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
COL2 DATE FORMAT 'yyyy-mm-dd',
COL3 VARCHAR(12) CHARACTER SET LATIN NOT CASESPECIFIC

然而,在恢复之后,视图从未刷新过。

REPLACE VIEW MAIN_DB.TABLE_A AS LOCKING ROW FOR ACCESS SELECT * FROM MAIN_DB_T.TABLE_A;

所以尽管一切都应该如此。这个观点正在创造所有的大惊小怪。我刷新了视图,效果很好。