我试图在Java中的IF-THEN-ELSE语句中创建一个表。 这是我到目前为止所尝试的内容:
stmt.execute(
"BEGIN IF (SELECT tabname FROM syscat.tables WHERE tabname = 'KUNDENKONTAKTDATEN' and tabschema = 'GRP35') IS NULL THEN "
+ "CREATE TABLE KundenKontaktDaten(Kunden_Nr INT NOT NULL PRIMARY KEY, Twitter_Id VARCHAR(40), Google_Id BIGINT, Facebook_Id BIGINT, Skype_Id VARCHAR(64), Telefonnummer VARCHAR(50));"
+ "ELSE "
+ "DELETE FROM KundenKontaktDaten; "
+ "END IF;"
+ "END");
运行这个我总是得到以下异常:
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=KEY;INT NOT NULL PRIMARY;<references_spec>, DRIVER=4.13.80
从Kunden_Nr中删除NOT NULL和PRIMARY KEY会返回以下异常:
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=<ddl-statement>, DRIVER=4.13.80
有没有人知道如何处理那个?
招呼
答案 0 :(得分:0)
大多数DDL语句(包括CREATE TABLE
)无法在独立的复合SQL中编译(您可以将其称为匿名块)。您需要动态执行它:
stmt.execute(
"BEGIN IF (SELECT tabname FROM syscat.tables WHERE tabname = 'KUNDENKONTAKTDATEN' and tabschema = 'GRP35') IS NULL THEN "
+ "EXECUTE IMMEDIATE 'CREATE TABLE KundenKontaktDaten (...)';"
+ ...