所以我试图做一个简单的程序,但是我的Insert出现错误,我也不知道为什么。我已经创建了序列和表(Lab1_tab),但此处未包括它们。入学表和学生表都可以正常工作。我的错误是在第18行的第一个插入上。确切的错误是PLS-00103:遇到符号“ INSERT”
DECLARE
v_lname VARCHAR(20);
v_id NUMBER(3);
BEGIN
SELECT s.last_name
INTO v_lname
FROM enrollment e, student s
WHERE e.student_id = s.student_id
AND LENGTH(s.last_name) < 9
GROUP BY s.last_name
HAVING COUNT(*) = ( SELECT MAX(count(student_id))
FROM enrollment
GROUP BY student_id);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_lname := 'Multiple Names';
END;
INSERT INTO Lab1_tab //ERROR
VALUES(LAB1_SEQ.NEXTVAL, v_lname);
BEGIN
SELECT s.last_name
INTO v_lname
FROM enrollment e, student s
WHERE e.student_id = s.student_id
AND LENGTH(s.last_name) < 9
GROUP BY s.last_name
having count(*) = ( SELECT MIN(count(student_id))
FROM enrollment
GROUP BY student_id);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_lname := 'Multiple Names';
END;
INSERT INTO Lab1_tab
VALUES (LAB1_SEQ.NEXTVAL, v_lname);
BEGIN
SELECT i.last_name
INTO v_lname
FROM instructor i, section s
WHERE s.instructor_id = i.instructor_id
AND i.last_name NOT LIKE '%s'
GROUP BY i.last_name
having count(*) = ( SELECT MIN(count(*))
FROM section
GROUP BY instructor_id );
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_lname := 'Multiple Names';
END;
INSERT INTO Lab1_tab
VALUES (1, v_lname);
BEGIN
SELECT i.last_name
INTO v_lname
FROM instructor i, section s
WHERE s.instructor_id = i.instructor_id
AND i.last_name NOT LIKE '%s'
GROUP BY i.last_name
having count(*) = ( SELECT MAX(count(*))
FROM section
GROUP BY instructor_id );
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_lname := 'Multiple Names';
END;
INSERT INTO Lab1_tab
VALUES (LAB1_SEQ.NEXTVAL, v_lname);
END;
/
答案 0 :(得分:4)
看来,您正在尝试执行PL / SQL匿名块,但是很快就关闭了该块。即第一个END
,您可能打算关闭“ try-catch”块,实际上关闭了整个容器。
也许您对DECLARE
感到困惑,该BEGIN
正在启动匿名块,但不是开始一个代码块-您仍然必须拥有DECLARE
v_lname VARCHAR(20);
v_id NUMBER(3);
BEGIN
BEGIN
SELECT s.last_name
INTO v_lname
FROM enrollment e, student s
WHERE e.student_id = s.student_id
AND LENGTH(s.last_name) < 9
GROUP BY s.last_name
HAVING COUNT(*) = ( SELECT MAX(count(student_id))
FROM enrollment
GROUP BY student_id);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_lname := 'Multiple Names';
END;
INSERT INTO Lab1_tab //ERROR
VALUES(LAB1_SEQ.NEXTVAL, v_lname);
BEGIN
SELECT s.last_name
INTO v_lname
FROM enrollment e, student s
WHERE e.student_id = s.student_id
AND LENGTH(s.last_name) < 9
GROUP BY s.last_name
having count(*) = ( SELECT MIN(count(student_id))
FROM enrollment
GROUP BY student_id);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_lname := 'Multiple Names';
END;
INSERT INTO Lab1_tab
VALUES (LAB1_SEQ.NEXTVAL, v_lname);
BEGIN
SELECT i.last_name
INTO v_lname
FROM instructor i, section s
WHERE s.instructor_id = i.instructor_id
AND i.last_name NOT LIKE '%s'
GROUP BY i.last_name
having count(*) = ( SELECT MIN(count(*))
FROM section
GROUP BY instructor_id );
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_lname := 'Multiple Names';
END;
INSERT INTO Lab1_tab
VALUES (1, v_lname);
BEGIN
SELECT i.last_name
INTO v_lname
FROM instructor i, section s
WHERE s.instructor_id = i.instructor_id
AND i.last_name NOT LIKE '%s'
GROUP BY i.last_name
having count(*) = ( SELECT MAX(count(*))
FROM section
GROUP BY instructor_id );
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_lname := 'Multiple Names';
END;
INSERT INTO Lab1_tab
VALUES (LAB1_SEQ.NEXTVAL, v_lname);
END;
/
。
尝试这样的事情:
BEGIN
修改:
总而言之,如果有疑问,请计算您的END
和{{1}}并查看数字是否匹配。 :)
答案 1 :(得分:0)
在PL / SQL匿名阻止之后,需要有一个/
字符才能运行DDL语句。