如果UCANACESS不存在

时间:2019-07-17 10:57:12

标签: sql ms-access jdbc ucanaccess

我有以下代码,但是UCanAccess不允许我使用IF NOT EXISTS。有人有什么想法吗?

"if not exists (select ArtikelNr from Artikel where ArtikelNr='"+ValueRead.prüfen1()+"')" + 
                "begin" + 
                "    if not exists (select isa_id from Artikel where isa_id='"+Integer.parseInt(Data.textField.getText())+"')" + 
                "    begin" + 
                "INSERT INTO Artikel ([HERE ARE THE TABLES) VALUES 

[HERE ARE THE VALUES]

                "    end" + 
                "end";

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

// test data
int artikelNr = 123;
int isa_id = 1234;
String description = "artikel_123";

String sql = ""
        + "INSERT INTO Artikel (ArtikelNr, isa_id, description) "
        + "SELECT ? AS ArtikelNr, ? AS isa_id, ? as description "
        + "FROM DUAL "
        + "WHERE NOT EXISTS ("
        + "    SELECT * FROM Artikel "
        + "    WHERE ArtikelNr=? AND isa_id=?)";
PreparedStatement ps = conn.prepareStatement(sql);

// values to INSERT
ps.setInt(1, artikelNr);
ps.setInt(2, isa_id);
ps.setString(3, description);

// values for WHERE NOT EXISTS
ps.setInt(4, artikelNr);
ps.setInt(5, isa_id);

int rtn = ps.executeUpdate();
System.out.printf("%d row(s) inserted%n", rtn);