PL/SQL 检查输入的值是否存在于不同的 table
PL/SQL check to see if an inputted value exists in a different table
目标:
验证 storeName 存在于 storeLocation table 中并验证 invoiceNumber 不存在于 invoiceHistory table.
使用下面的代码,我可以使用 insert into 添加到 table,但是当我使用 where exists and not exists 时,出现以下错误:
ORA-06550:第 7 行,第 5 列:
PL/SQL: ORA-00933: SQL 命令未正确结束
ACCEPT storename PROMPT 'Enter StoreName: '
ACCEPT price PROMPT 'Enter Price: '
ACCEPT tax PROMPT 'Enter Tax : '
ACCEPT total PROMPT 'Enter the total: '
ACCEPT invoicenumber PROMPT 'Enter invoice number: '
BEGIN
INSERT INTO order VALUES (
'&storename ',
'&price ',
'&tax',
'&total ',
'&invoicenumber ')
WHERE
EXISTS(
SELECT * FROM storelocation
where upper(storelocation.storename) = upper('&storename '))
AND NOT EXISTS(
SELECT * FROM invoiceHistory
where invoiceHistory.invoicenumber = '&invoicenumber ')
);
COMMIT;
END;
为什么会出现此错误,我该如何避免?
INSERT ... VALUES ...
不能有 WHERE
子句。但是 INSERT ... SELECT ...
可以。
INSERT INTO order
SELECT '&storename ',
'&price ',
'&tax',
'&total ',
'&invoicenumber '
FROM dual
WHERE EXISTS (SELECT *
FROM storelocation
WHERE upper(storelocation.storename) = upper('&storename '))
AND NOT EXISTS (SELECT *
FROM invoicehistory
WHERE invoicehistory.invoicenumber = '&invoicenumber ');
但是您应该养成在 INSERT
中明确列出目标列的习惯。这确保一切都在它应该去的地方。
(您也可以检查您是否真的 want/need 字符串文字中的尾随 space。)
目标: 验证 storeName 存在于 storeLocation table 中并验证 invoiceNumber 不存在于 invoiceHistory table.
使用下面的代码,我可以使用 insert into 添加到 table,但是当我使用 where exists and not exists 时,出现以下错误:
ORA-06550:第 7 行,第 5 列:
PL/SQL: ORA-00933: SQL 命令未正确结束
ACCEPT storename PROMPT 'Enter StoreName: '
ACCEPT price PROMPT 'Enter Price: '
ACCEPT tax PROMPT 'Enter Tax : '
ACCEPT total PROMPT 'Enter the total: '
ACCEPT invoicenumber PROMPT 'Enter invoice number: '
BEGIN
INSERT INTO order VALUES (
'&storename ',
'&price ',
'&tax',
'&total ',
'&invoicenumber ')
WHERE
EXISTS(
SELECT * FROM storelocation
where upper(storelocation.storename) = upper('&storename '))
AND NOT EXISTS(
SELECT * FROM invoiceHistory
where invoiceHistory.invoicenumber = '&invoicenumber ')
);
COMMIT;
END;
为什么会出现此错误,我该如何避免?
INSERT ... VALUES ...
不能有 WHERE
子句。但是 INSERT ... SELECT ...
可以。
INSERT INTO order
SELECT '&storename ',
'&price ',
'&tax',
'&total ',
'&invoicenumber '
FROM dual
WHERE EXISTS (SELECT *
FROM storelocation
WHERE upper(storelocation.storename) = upper('&storename '))
AND NOT EXISTS (SELECT *
FROM invoicehistory
WHERE invoicehistory.invoicenumber = '&invoicenumber ');
但是您应该养成在 INSERT
中明确列出目标列的习惯。这确保一切都在它应该去的地方。
(您也可以检查您是否真的 want/need 字符串文字中的尾随 space。)