在INSERT IN STATEMENT SQL中使用CAST STATEMENT

时间:2019-10-10 12:55:35

标签: sql casting sql-insert vertica

下面的(1)是我编写的查询,当我不使用CAST时,我会收到错误(2),而当使用强制转换时,它会抛出另一个语法错误(3)

(1)

 /*DROP TABLE IF EXISTS LFB1_BACKUPTABLE
 CREATE TABLE LFB1_BACKUPTABLE AS   
(SELECT * FROM LFB1);
*/


INSERT INTO LFB1_BACKUPTABLE (
    MANDT,LIFNR,BUKRS,PERNR,ERDAT,ERNAM,SPERR,LOEVM,ZUAWA,
    AKONT,BEGRU,VZSKZ,ZWELS,XVERR,ZAHLS,ZTERM,EIKTO,ZSABE,
    KVERM,FDGRV,BUSAB,LNRZE,LNRZB,ZINDT,ZINRT,DATLZ,XDEZV,
    WEBTR,KULTG,REPRF,TOGRU,HBKID,XPORE,QSZNR,QSZDT,QSSKZ,
    BLNKZ,MINDK,ALTKN,ZGRUP,MGRUP,UZAWE,QSREC,QSBGR,QLAND,
    XEDIP,FRGRP,TOGRR,TLFXS,INTAD,XLFZB,GUZTE,GRICD,GRIDT,
    XAUSZ,CERDT,CONFS,UPDAT,UPTIM,NODEL,TLFNS,AVSND,AD_HASH,
    J_SC_SUBCONTYPE,J_SC_COMPDATE,J_SC_OFFSM,J_SC_OFFSR,
    BASIS_PNT,GMVKZK,PREPAY_RELEVANT,ASSIGN_TEST, CAST(_CELONIS_CHANGE_DATE AS DATE) AS _CELONIS_CHANGE_DATE 
)
SELECT DISTINCT * FROM LFB1
WHERE MANDT||LIFNR||BUKRS NOT IN (SELECT DISTINCT MANDT||LIFNR||BUKRS FROM 
LFB1_BACKUPTABLE);

(2)执行错误:

  

[Vertica] VJDBC错误:“ _ CELONIS_CHANGE_DATE”列的类型为timestamptz,但表达式的类型为varchar

(3)执行错误:

  

[Vertica] VJDBC错误:“ CAST”处或附近的语法错误

1 个答案:

答案 0 :(得分:0)

为什么在插入表列列表中使用强制转换语句?您无法更改目标表列的类型。

您需要更改要插入的值的类型。

INSERT INTO LFB1_BACKUPTABLE (
        MANDT,LIFNR,BUKRS,PERNR,ERDAT,ERNAM,SPERR,LOEVM,ZUAWA,
        AKONT,BEGRU,VZSKZ,ZWELS,XVERR,ZAHLS,ZTERM,EIKTO,ZSABE,
        KVERM,FDGRV,BUSAB,LNRZE,LNRZB,ZINDT,ZINRT,DATLZ,XDEZV,
        WEBTR,KULTG,REPRF,TOGRU,HBKID,XPORE,QSZNR,QSZDT,QSSKZ,
        BLNKZ,MINDK,ALTKN,ZGRUP,MGRUP,UZAWE,QSREC,QSBGR,QLAND,
        XEDIP,FRGRP,TOGRR,TLFXS,INTAD,XLFZB,GUZTE,GRICD,GRIDT,
        XAUSZ,CERDT,CONFS,UPDAT,UPTIM,NODEL,TLFNS,AVSND,AD_HASH,
        J_SC_SUBCONTYPE,J_SC_COMPDATE,J_SC_OFFSM,J_SC_OFFSR,
        BASIS_PNT,GMVKZK,PREPAY_RELEVANT,ASSIGN_TEST, _CELONIS_CHANGE_DATE
)
SELECT DISTINCT col1, col2, col3, ..., timestamp_col::DATE FROM LFB1
WHERE MANDT||LIFNR||BUKRS NOT IN (SELECT DISTINCT MANDT||LIFNR||BUKRS FROM LFB1_BACKUPTABLE);

您用于强制转换为日期的语法在语法上是正确的,但是在本示例中,我使用了更常见的Vertica语法,即col::DATE,两者都可以使用。