有外键时记录错误失败

时间:2018-03-09 19:57:01

标签: teradata error-logging

当存在外键约束时,我无法使用LOGGING ERRORS功能。我做错了什么?

以下是我的例子:

/*
DROP TABLE DM_CALIDAD.AMK_ACCT_DTL_DATA;
DROP TABLE DM_CALIDAD.AMK_ACCT_DTL_ERR;
DROP TABLE DM_CALIDAD.AMK_ACCT_DTL;
DROP TABLE DM_CALIDAD.AMK_ACCT_ANC;
*/

-- Create parent table 
CREATE SET TABLE DM_CALIDAD.AMK_ACCT_ANC ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      ACCT_ID INTEGER NOT NULL,
      LOAD_TS TIMESTAMP(6) NOT NULL)
UNIQUE PRIMARY INDEX ACCT_ANC_UPI ( ACCT_ID );

-- Create child table
CREATE SET TABLE DM_CALIDAD.AMK_ACCT_DTL ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      ACCT_ID INTEGER NOT NULL,
      ACCT_DTL_ID INTEGER NOT NULL,
      LOAD_TS TIMESTAMP(6) NOT NULL, 
FOREIGN KEY ( ACCT_ID ) REFERENCES DM_CALIDAD.AMK_ACCT_ANC ( ACCT_ID ))
UNIQUE PRIMARY INDEX ACCT_DTL_UPI ( ACCT_ID ,ACCT_DTL_ID );

-- Create data table to use with insert/select. Same as child, but no FK
CREATE SET TABLE DM_CALIDAD.AMK_ACCT_DTL_DATA ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      ACCT_ID INTEGER NOT NULL,
      ACCT_DTL_ID INTEGER NOT NULL,
      LOAD_TS TIMESTAMP(6) NOT NULL)
UNIQUE PRIMARY INDEX ACCT_DTL_DATA_UPI ( ACCT_ID ,ACCT_DTL_ID );

-- Create error table
CREATE ERROR TABLE DM_CALIDAD.AMK_ACCT_DTL_ERR FOR DM_CALIDAD.AMK_ACCT_DTL;

--Insert data on parent and data tables.
INSERT INTO DM_CALIDAD.AMK_ACCT_ANC VALUES(1, current_timestamp);
INSERT INTO DM_CALIDAD.AMK_ACCT_DTL_DATA VALUES(1, 1, current_timestamp);
INSERT INTO DM_CALIDAD.AMK_ACCT_DTL_DATA VALUES(2, 1, current_timestamp); -- This will fail

--Insert/select into the child table
INSERT INTO DM_CALIDAD.AMK_ACCT_DTL 
  SELECT * FROM DM_CALIDAD.AMK_ACCT_DTL_DATA 
  LOGGING ALL ERRORS WITH NO LIMIT;

-- The insert/select fails with the next error
--STATEMENT 1: Insert Statement failed.  Failed [9127 : HY000] Index violations detected; errors logged in AMK_ACCT_DTL_ERR where ETC_dbql_qid = 307181783828932305.

如何将有效行插入子表中,将无效行插入错误表?

谢谢!

1 个答案:

答案 0 :(得分:0)

Logging Errors不支持FK违规行为,请参阅Logging Errors For INSERT … SELECT Requests的手册

When Teradata Database encounters USI or RI errors (or both) in the INSERT … SELECT operation, the following events occur in sequence:
1. The transaction or request runs to completion.
2. The system writes all error-causing rows into the error table.
3. The system aborts the transaction or request.
4. The system rolls back the transaction or request.