'插入x选择y'结果在ora-01847

时间:2018-03-08 08:58:22

标签: sql oracle date

因此我尝试使用'插入x选择y'来尝试将行的值从一个表复制到另一个表。结构体。这两个表具有完全相同的结构。这些表包含Timestamp(6)列,我认为它会以某种方式导致它失败。

查看下面的查询:

INSERT INTO sapd SELECT sap.* FROM sap WHERE ID=:id

这会导致以下错误:

ORA-01847 day of month must be between 1 and last day of month 
Cause: The day of the month listed in a date is invalid for the 
specified month. The day of the month (DD) must be between 1 
and the number of days in that month.

我发现此错误的修正方法是将值转换为带有TO_DATE()的日期,但看到我正在做的事情,请选择*'我无法做到这一点。 我尝试从代码(.NET)和Oracle SQL Developer工具运行此查询,两者都具有相同的结果。

有人知道如何解决这个问题吗?

表定义(只有一个导致另一个相同)。 (是的,我知道桌子有点大,但它是一个遗留项目。)

CREATE TABLE sapd
   (    "ID_STAGEPLAATS" NUMBER, 
    "STUDIEJAAR" NVARCHAR2(9), 
    "STAGE_AFSTUDEREN" NVARCHAR2(35), 
    "POS_ADVIES" CHAR(1 BYTE), 
    "BEVESTIGD" CHAR(1 BYTE), 
    "BEGIN" TIMESTAMP (6), 
    "EIND" TIMESTAMP (6), 
    "AANTAL_DAGEN" NUMBER, 
    "ID_BEDRIJF" NUMBER, 
    "AFDELING" NVARCHAR2(100), 
    "VERDIEPING" NVARCHAR2(50), 
    "KAMERNR" NVARCHAR2(25), 
    "OPDRACHT_D" CLOB, 
    "ID_CONTPERS" NUMBER, 
    "TITEL" NVARCHAR2(15), 
    "BEGEL_VL" NVARCHAR2(25), 
    "BEGEL_VV" NVARCHAR2(10), 
    "BEGEL_ROEPNAAM" NVARCHAR2(25), 
    "BEGEL_NAAM" NVARCHAR2(50), 
    "BEGEL_MV" NVARCHAR2(1), 
    "TEL_BEGELEIDER" NVARCHAR2(50), 
    "FAX_BEGELEIDER" NVARCHAR2(50), 
    "E_MAIL_BEGELEIDER" NVARCHAR2(200), 
    "MOBIEL_BEGELEIDER" NVARCHAR2(50), 
    "STUDENTENNUMMER" NUMBER, 
    "TEL_STUDENT" NVARCHAR2(50), 
    "FAX_STUDENT" NVARCHAR2(50), 
    "E_MAIL_STUDENT" NVARCHAR2(50), 
    "WOONADRES_STUDENT" NVARCHAR2(50), 
    "PC_STUDENT" NVARCHAR2(10), 
    "PLAATS_STUDENT" NVARCHAR2(50), 
    "TEL_STUDENT_TIJDELIJKE_THUIS" NVARCHAR2(15), 
    "DOCENT1" NVARCHAR2(50), 
    "DOCENT2" NVARCHAR2(50), 
    "THUISBERICHT" TIMESTAMP (6), 
    "PRES_DATUM" TIMESTAMP (6), 
    "PRES_TIJD" TIMESTAMP (6), 
    "PRES_PLAATS" NVARCHAR2(50), 
    "PRES_ONDERWERP" NVARCHAR2(255), 
    "BRDNG_BEDR" NUMBER, 
    "BRDNG_VRSLG" NUMBER, 
    "BRDNG_PRES" NUMBER, 
    "BRDNG_TOT" NUMBER, 
    "VERGOEDING" NUMBER, 
    "AFSPRAKEN_D" CLOB, 
    "OPMERKING_STUDENT_D" CLOB, 
    "OPMERKING_BEDRIJF_D" CLOB, 
    "STAGE_ADVIES" NVARCHAR2(5), 
    "VERWERVINGSBEDRIJF1_ID" NUMBER, 
    "VERWERVINGSBEDRIJF2_ID" NUMBER, 
    "AFSPRAKEN_BEDRIJF1_D" CLOB, 
    "AFSPRAKEN_BEDRIJF2_D" CLOB, 
    "BEVESTIGD_DATUM" TIMESTAMP (6), 
    "OPENSTAANDESTAGE_BEDRIJF" NUMBER, 
    "OS_VERWERVINGSBEDRIJF1" NUMBER, 
    "OS_VERWERVINGSBEDRIJF2" NUMBER, 
    "VALUTA" NVARCHAR2(10), 
    "STATUS_BEVESTIGING" NVARCHAR2(20), 
    "AANTAL_UREN" NUMBER, 
    "OPMERKING_D" CLOB, 
    "VOORKEURSDOCENT" NVARCHAR2(50), 
    "MOBIEL_STUDENT" NVARCHAR2(50), 
    "EXTRA1" NVARCHAR2(200), 
    "EXTRA2" NVARCHAR2(200), 
    "EXTRA3" NVARCHAR2(200), 
    "ORIGINELE__ID" NUMBER, 
    "AFGEMELD_VOLGPLUS" CHAR(1 BYTE), 
    "AANGEMAAKTDOOR" NVARCHAR2(100), 
    "AANGEMAAKTOP" TIMESTAMP (6), 
    "LAATSTGEWIJZIGDDOOR" NVARCHAR2(100), 
    "LAATSTGEWIJZIGDOP" TIMESTAMP (6), 
    "INSTITUUT" NVARCHAR2(50), 
    "ID_SIS" NUMBER, 
    "ID_SIS2" NUMBER(38,0), 
    "ID_VELDWERKER" NUMBER(38,0), 
    "ID_BEDRIJF_ORIGINEEL" NUMBER, 
    "ID_CONTACT_ORIGINEEL" NUMBER, 
    "STUDNR_ORI" NVARCHAR2(250), 
    "AANTAL_UREN_ORI" NUMBER(38,1), 
    "AANTAL_DAGEN_ORI" NUMBER(38,1), 
    "STAGE_VERPLAATST" CHAR(1 BYTE), 
    "REISSCHEMA_D" CLOB, 
    "AFSPRAKEN" NCLOB, 
    "AFSPRAKEN_BEDRIJF1" NCLOB, 
    "AFSPRAKEN_BEDRIJF2" NCLOB, 
    "OPDRACHT" NCLOB, 
    "OPMERKING" NCLOB, 
    "OPMERKING_BEDRIJF" NCLOB, 
    "OPMERKING_STUDENT" NCLOB, 
    "REISSCHEMA" NCLOB, 
    "EXTRA4" NVARCHAR2(200), 
    "EXTRA5" NVARCHAR2(200), 
    "OPMERKING_VAN_BEDRIJF" NCLOB, 
    "LAND_STUDENT" NVARCHAR2(50), 
    "EXTRA_INFO" NVARCHAR2(50), 
    "ST_VIEWAGREEMENT" CHAR(1 BYTE) DEFAULT 0, 
    "BESCHIKBAAR_MA" CHAR(1 BYTE) DEFAULT 0, 
    "BESCHIKBAAR_DI" CHAR(1 BYTE) DEFAULT 0, 
    "BESCHIKBAAR_WO" CHAR(1 BYTE) DEFAULT 0, 
    "BESCHIKBAAR_DO" CHAR(1 BYTE) DEFAULT 0, 
    "BESCHIKBAAR_VR" CHAR(1 BYTE) DEFAULT 0, 
    "STUDENT_CHANGED_INFO" CHAR(1 BYTE) DEFAULT 0, 
    "STUDENT_CHANGED_INFO_DATE" TIMESTAMP (6), 
    "BEG_2_TITEL" NVARCHAR2(15), 
    "BEG_2_VL" NVARCHAR2(25), 
    "BEG_2_VV" NVARCHAR2(10), 
    "BEG_2_ROEPNAAM" NVARCHAR2(25), 
    "BEG_2_NAAM" NVARCHAR2(50), 
    "BEG_2_MV" NVARCHAR2(1), 
    "BEG_2_TEL" NVARCHAR2(50), 
    "BEG_2_FAX" NVARCHAR2(50), 
    "BEG_2_E_MAIL" NVARCHAR2(200), 
    "BEG_2_MOBIEL" NVARCHAR2(50), 
    "BEG_BESCHIKBAAR_MA" CHAR(1 BYTE), 
    "BEG_BESCHIKBAAR_DI" CHAR(1 BYTE), 
    "BEG_BESCHIKBAAR_WO" CHAR(1 BYTE), 
    "BEG_BESCHIKBAAR_DO" CHAR(1 BYTE), 
    "BEG_BESCHIKBAAR_VR" CHAR(1 BYTE), 
    "BEG_2_BESCHIKBAAR_MA" CHAR(1 BYTE), 
    "BEG_2_BESCHIKBAAR_DI" CHAR(1 BYTE), 
    "BEG_2_BESCHIKBAAR_WO" CHAR(1 BYTE), 
    "BEG_2_BESCHIKBAAR_DO" CHAR(1 BYTE), 
    "BEG_2_BESCHIKBAAR_VR" CHAR(1 BYTE), 
    "MERGE_INSTITUUT" NVARCHAR2(100), 
    "MOTIVATION_STUDENT" NCLOB, 
    "ROB_FROM" NVARCHAR2(10), 
    "ROB_TO" NVARCHAR2(10), 
    "ROB_ID" NVARCHAR2(10), 
    "STUDENTNR_ITSIS" NUMBER, 
    "INSTITUUT_SLHAPO" NVARCHAR2(50), 
    "STUDENTNR_VOOR_ITSIS" VARCHAR2(20 BYTE), 
    "BESCHIKBAAR_ZA" CHAR(1 BYTE) DEFAULT 0, 
    "BESCHIKBAAR_ZO" CHAR(1 BYTE) DEFAULT 0, 
    "BEG_BESCHIKBAAR_ZA" CHAR(1 BYTE), 
    "BEG_BESCHIKBAAR_ZO" CHAR(1 BYTE), 
    "BEG_2_BESCHIKBAAR_ZA" CHAR(1 BYTE), 
    "BEG_2_BESCHIKBAAR_ZO" CHAR(1 BYTE), 
    "EXTRA6" NVARCHAR2(200), 
    "EXTRA7" NVARCHAR2(200), 
    "EXTRA8" NVARCHAR2(200), 
    "EXTRA9" NVARCHAR2(200), 
    "EXTRA10" NVARCHAR2(200), 
    "EXTRA11" NVARCHAR2(200), 
    "EXTRA12" NVARCHAR2(200), 
    "EXTRA13" NVARCHAR2(200), 
    "EXTRA14" NVARCHAR2(200), 
    "EXTRA15" NVARCHAR2(200), 
    "EXTRA_CHAR1" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR2" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR3" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR4" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR5" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR6" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR7" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR8" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR9" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_CHAR10" CHAR(1 BYTE) DEFAULT 0, 
    "EXTRA_DATE1" TIMESTAMP (6), 
    "EXTRA_DATE2" TIMESTAMP (6), 
    "EXTRA_DATE3" TIMESTAMP (6), 
    "EXTRA_DATE4" TIMESTAMP (6), 
    "EXTRA_DATE5" TIMESTAMP (6), 
    "EXTRA_DATE6" TIMESTAMP (6), 
    "EXTRA_DATE7" TIMESTAMP (6), 
    "EXTRA_DATE8" TIMESTAMP (6), 
    "EXTRA_DATE9" TIMESTAMP (6), 
    "EXTRA_DATE10" TIMESTAMP (6), 
    "ONDERNEMINGSZIN" NCLOB, 
    "EXTRA_DATE11" TIMESTAMP (6), 
    "EXTRA_DATE12" TIMESTAMP (6), 
    "EXTRA_DATE13" TIMESTAMP (6), 
    "REVIEW_INGEVULD" CHAR(1 BYTE), 
    "REVIEW_REMINDER" CHAR(1 BYTE), 
    "REVIEW_GUID" NVARCHAR2(100), 
    "CUSTOM_INTERNSHIP_DAYS" NVARCHAR2(255), 
    "LWO_REMINDER_SENT" TIMESTAMP (6), 
    "REASON" NCLOB, 
    "REJECTOR_ID" NUMBER, 
    "REJECTOR_TYPE" NUMBER, 
    "OPLEIDING" NVARCHAR2(100)
   )

2 个答案:

答案 0 :(得分:1)

避免SELECT *的一个解决方案是生成如下的动态SQL语句:

DECLARE
    sqlstr VARCHAR2(30000);
BEGIN
    sqlstr := 'INSERT INTO sapd ('
    FOR aCol IN (SELECT column_name FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'SAPD' ORDER BY COLUMN_ID) LOOP
        sqlstr := sqlstr || aCol.column_name||',';
    END LOOP;
    sqlstr := REGEXP_REPLACE(sqlstr, ',$', ')');

    sqlstr := sqlstr || ' SELECT ';
    FOR aCol IN (SELECT column_name FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'SAPD' ORDER BY COLUMN_ID) LOOP
        sqlstr := sqlstr || aCol.column_name||',';
    END LOOP;
    sqlstr := REGEXP_REPLACE(sqlstr, ',$');

    sqlstr := sqlstr || ' FROM sap WHERE ID = :id';

    EXECUTE IMMEDIATE sqlstr using id;
end;

答案 1 :(得分:0)

正如Damien所说,表定义中确实存在订单不匹配。这会导致错误

假设两个表定义1和2具有列a,b,c和d;但是对于表1,它按顺序a,b,c,d,表2按顺序a,c,b,d。如果我现在尝试做

INSERT INTO Table_1 SELECT * FROM Table_2

然后表2中的c值将被放入Table_1中的b值。这会导致问题。

对于我自己的问题,它会尝试将非日期值放入日期列。