我尝试创建一个允许我在shipment_value表中添加新记录的程序,但我不想添加任何已存在的记录。所以我使用了WHERE NOT EXISTS条件,但我收到了一些编译错误。
CREATE OR REPLACE PROCEDURE Add_Shipment_Method(
shipment_method_id_p IN NUMBER,
shipment_description_p IN VARCHAR2)
AS
BEGIN
INSERT INTO shipment_method VALUES(shipment_method_id_p, shipment_description_p)
WHERE NOT EXISTS (SELECT * FROM SHIPMENT_METHOD WHERE SHIPMENT_METHOD_ID = shipment_method_id_p AND SHIPMENT_DESCRIPTION = shipment_description_p);
COMMIT;
END;
我的程序在添加" NOT EXISTS"之前运行良好,但现在我有编译错误
答案 0 :(得分:1)
要使用不存在,您必须插入查询结果,而不是值列表。
如果您的数据库是oracle,您可以像这样使用
来插入插件INSERT INTO shipment_method
SELECT shipment_method_id_p, shipment_description_p
FROM dual
WHERE NOT EXISTS (SELECT * FROM SHIPMENT_METHOD WHERE SHIPMENT_METHOD_ID = shipment_method_id_p AND SHIPMENT_DESCRIPTION = shipment_description_p);
或者如果您有唯一索引,则可以捕获异常。