出现以下错误:ORA-01422:精确提取返回的行数超过了请求的行数 ORA-6512
PL/SQL 的新手,虽然我知道这个查询应该通过标准 SQL 进行,但我试图弄清楚如何使用 PL/SQL 来完成这个。
我需要查询返回邮政编码为“20636”的客户的销售总数
这是表格/输入:
CREATE TABLE CUSTOMERS
(customerID INT PRIMARY KEY,
customerZip VARCHAR(15) NOT NULL);
CREATE TABLE SALES
(saleID INT PRIMARY KEY,
customerID INT,
CONSTRAINT SALES_FK1 FOREIGN KEY (customerID) REFERENCES CUSTOMERS(customerID));
INSERT INTO CUSTOMERS (customerID, customerZIP) VALUES (1, '20636');
INSERT INTO CUSTOMERS (customerID, customerZIP) VALUES (2, '20619');
INSERT INTO CUSTOMERS (customerID, customerZIP) VALUES (3, '20650');
INSERT INTO CUSTOMERS (customerID, customerZIP) VALUES (4, '20670');
INSERT INTO CUSTOMERS (customerID, customerZIP) VALUES (5, '20636');
INSERT INTO SALES (saleID, customerID) VALUES (1, 1);
INSERT INTO SALES (saleID, customerID) VALUES (2, 2);
INSERT INTO SALES (saleID, customerID) VALUES (3, 3);
INSERT INTO SALES (saleID, customerID) VALUES (4, 4);
INSERT INTO SALES (saleID, customerID) VALUES (5, 5);
这是我为 PL/SQL 匿名块编写的代码:
DECLARE
customerZip INTEGER;
totalSales INTEGER;
BEGIN
SELECT customerID INTO customerZip from CUSTOMERS where customerZip = '20636';
SELECT COUNT(*) INTO totalSales from SALES where customerID = customerZip;
DBMS_OUTPUT.put_line('We sold ' || totalSales || ' Cars to customers in Zip Code ' || customerZip ||'.');
END;
/
如果我在没有将两个 customerZIP 属性设置为“20636”的情况下运行它,它可以正常工作。一旦我输入多个邮政编码为“20636”的客户记录,我就会收到错误消息。
您能否解释一下我在这里做错了什么以及如何解决?谢谢!
SQL Fiddle 链接(如果有帮助):http ://sqlfiddle.com/#!4/10fc1