При заполнении параметров подстановка productName идёт напрямую, без кавычек, to_char не работает Мой код:
MERGE INTO STORE_PRODUCTS ps
USING (
SELECT :storeId AS store_id,
p.ID AS product_id,
:productCount AS product_count
FROM products p
WHERE p.NAME = :productName
) src
ON (ps.storeid = src.store_id AND ps.productid = src.product_id)
WHEN MATCHED THEN
UPDATE SET ps.COUNTPRODUCT = PS.COUNTPRODUCT + src.product_count
WHEN NOT MATCHED THEN
INSERT (storeid, productid, COUNTPRODUCT)
VALUES (src.store_id, src.product_id, src.product_count);
Вывод консоли:
MERGE INTO STORE_PRODUCTS ps
USING (
SELECT 1 AS store_id,
p.ID AS product_id,
10 AS product_count
FROM products p
WHERE p.NAME = CARS
) src
ON (ps.storeid = src.store_id AND ps.productid = src.product_id)
WHEN MATCHED THEN
UPDATE SET ps.COUNTPRODUCT = PS.COUNTPRODUCT + src.product_count
WHEN NOT MATCHED THEN
INSERT (storeid, productid, COUNTPRODUCT)
VALUES (src.store_id, src.product_id, src.product_count)
[2024-09-03 23:21:29] [42000][904] ORA-00904: "CARS": недопустимый идентификатор
[2024-09-03 23:21:29] Position: 170
JAVA код выглядит так, при выполнении код зависает, ошибки не выдаёт
public void mergeProducts(int storeId, ProductEnum productEnum, int countProduct) {
String sql = "MERGE INTO STORE_PRODUCTS ps\n" +
"USING (\n" +
" SELECT ? AS store_id,\n" +
" p.ID AS product_id,\n" +
" ? AS product_count\n" +
" FROM products p\n" +
" WHERE p.NAME = ?\n" +
") src\n" +
"ON (ps.storeid = src.store_id AND ps.productid = src.product_id)\n" +
"WHEN MATCHED THEN\n" +
" UPDATE SET ps.COUNTPRODUCT = PS.COUNTPRODUCT + src.product_count\n" +
"WHEN NOT MATCHED THEN\n" +
" INSERT (storeid, productid, COUNTPRODUCT)\n" +
" VALUES (src.store_id, src.product_id, src.product_count)";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, storeId);
statement.setInt(2, countProduct);
statement.setString(3, productEnum.toString());
statement.executeUpdate();
} catch (SQLException e) {
System.err.println("Error merge " + e.getMessage());
}
}