0

I have installed teradatasql python module recently.When I am doing batch insert into table it is not throwing duplicate error in the script, Else it is skipping that insert statement. Table has first column as UNIQUE in teradata table. But I want it to throw an error in the code.

with teradatasql.connect ('{"host":"whomooz","user":"guest","password":"please"}') as con:
    with con.cursor () as cur:
        cur.fast_executemany=True
        cur.execute ("insert into voltab (?, ?)", [
            [1, "abc"],
            [2, "def"],
            [3, "ghi"]])
rocks
  • 13
  • 3

1 Answers1

0

You need to use the escape functions teradata_nativesql and teradata_get_errors to obtain errors for unique column violations.

This example Python script:

import teradatasql
with teradatasql.connect (host="whomooz", user="guest", password="please") as con:
    with con.cursor () as cur:
        cur.execute ("create volatile table voltab (c1 integer unique not null, c2 varchar(10)) on commit preserve rows")
        sInsert = "insert into voltab (?, ?)"
        cur.execute (sInsert, [[123, "abc"], [123, "def"]])
        cur.execute ("{fn teradata_nativesql}{fn teradata_get_errors}" + sInsert)
        print (cur.fetchone () [0])

Prints the following output:

[Version 17.20.0.14] [Session 1080] [Teradata Database] [Error 2801] Batch request failed to execute 1 of 2 batched statements. Batched statement 2 failed to execute because of Teradata Database error 2801, Duplicate unique prime key error in guest.voltab.
 at gosqldriver/teradatasql.formatError ErrorUtil.go:89
 at gosqldriver/teradatasql.(*teradataConnection).formatDatabaseError ErrorUtil.go:217
 ...
Tom Nolan
  • 394
  • 2
  • 5
  • It is throwing error now. But I want to know the data for which it failed. Atleast the key for which it failed @TomNolan – rocks Feb 15 '23 at 04:25
  • The database does not tell the driver which key value failed. The database only tells the driver which row failed, and that is what the driver indicates in the error message: "Batched statement 2 failed to execute because of Teradata Database error 2801, Duplicate unique prime key error in guest.voltab." – Tom Nolan Feb 15 '23 at 20:41