我正在尝试结合使用 PL/SQL 和 SQL 命令来操作数据库。我做得对吗?任何帮助和更正?

i am trying to combine the PL/SQL and the SQL commands in manipulating the database. am i doing it correctly? any help and corrections?

enter image description hereuse PL/SQL with SQL commands in manipulating the database (PL/SQL structure, Data types, Variable, DBMS Output and Conditions) 这是我的代码和什么我明白了enter image description here

CREATE TABLE Grades(
    STUDENT_ID INT NOT NULL PRIMARY KEY, 
    SName VARCHAR2(50),
    Grades NUMBER,
    Top_Average NUMBER(3)
);

INSERT INTO Grades(STUDENT_ID, SName, Grades, Top_Average)VALUES(1001,'Carmina',75);
INSERT INTO Grades(STUDENT_ID, SName, Grades, Top_Average)VALUES(1002,'Danielle',90,100);
INSERT INTO Grades(STUDENT_ID, SName, Grades, Top_Average)VALUES(1003,'Sophia',80,100);
INSERT INTO Grades(STUDENT_ID, SName, Grades, Top_Average)VALUES(1004,'Zoe',85,100);
INSERT INTO Grades(STUDENT_ID, SName, Grades, Top_Average)VALUES(1005,'Carlo',78,100);

SELECT * FROM Grades;

DECLARE
    Grades NUMBER:= 75;
    Top_Average NUMBER:= 100;
   
BEGIN
    dbms_output.put_line('The sum of ' || Grades ||' and ' || Top_Average || ' is ' || (Grades + Top_Average));
    dbms_output.put_line('The difference of ' || Grades ||' and ' || Top_Average || ' is ' || (Grades - Top_Average));
    dbms_output.put_line('The product of ' || Grades ||' and ' || Top_Average || ' is ' || (Grades * Top_Average));
    
if Top_Average = grades then
    dbms_output.put_line('The quotient of ' || Grades ||' and ' || Top_Average|| ' is ' || ' FAILED');
    
ELSE
    dbms_output.put_line('The quotient of ' || Grades ||' and ' || Top_Average || ' is ' || (Grades / Top_Average)); 
end if;   
end; 
/

我的示例代码没有 运行。它给出

INSERT INTO Grades(STUDENT_ID, SName, Grades, Top_Average)VALUES(1001,'Carmina',75)
Error at Command Line : 8 Column : 59
Error report -
SQL Error: ORA-00947: not enough values
00947. 00000 -  "not enough values"

原因是您的语句中有 4 列,但第一个插入语句的“值”部分中只有 3 个值。删除“top_average”列或添加一个值。

但是,该问题与您遇到的错误无关。屏幕截图泄露了它。您正在select输入以下 3 行

DECLARE
    Grades NUMBER:= 75;
    Top_Average NUMBER:= 100;

然后 运行将其作为声明。如果您在 sqldeveloper 中 select 多行,它将尝试 运行 那些 selected 行仅 作为语句。这不是一个完整的陈述,所以它失败了。

来解决。将光标置于 pl/sql 块和 运行 语句之前或之中。它 运行 没有错误,但它不显示任何内容,因为 set serveroutput on 在块之前丢失。

这是格式正确的代码

  • 正确识别pl/sql
  • 将关键字更改为大写,将标识符更改为小写(为了便于阅读)
  • pl/sql 中的重命名变量 - 见评论。
DROP TABLE grades;

CREATE TABLE grades(
    student_id INT NOT NULL PRIMARY KEY, 
    sname VARCHAR2(50),
    grades NUMBER,
    top_average NUMBER(3)
);

INSERT INTO grades(student_id, sname, grades) VALUES (1001,'Carmina',75);
INSERT INTO grades(student_id, sname, grades,top_average) VALUES (1002,'Danielle',90,100);
INSERT INTO grades(student_id, sname, grades,top_average) VALUES (1003,'Sophia',80,100);
INSERT INTO grades(student_id, sname, grades,top_average) VALUES (1004,'Zoe',85,100);
INSERT INTO grades(student_id, sname, grades,top_average) VALUES (1005,'Carlo',78,100);

SELECT * FROM grades;

set serveroutput on size 999999
--clear screen
DECLARE
-- do NOT name your variables the same as your column names. It's confusing. Instead prefix with "l_" (local variable) 
    l_grades NUMBER:= 75;
    l_top_average NUMBER:= 100;
BEGIN
  dbms_output.put_line('The sum of ' || l_grades ||' and ' || l_top_average || ' is ' || (l_grades + l_top_average));
  dbms_output.put_line('The difference of ' || l_grades ||' and ' || l_top_average || ' is ' || (l_grades - l_top_average));
  dbms_output.put_line('The product of ' || l_grades ||' and ' || l_top_average || ' is ' || (l_grades * l_top_average));  
  IF l_top_average = l_grades THEN
    dbms_output.put_line('The quotient of ' || l_grades ||' and ' || l_top_average|| ' is ' || ' FAILED');
  ELSE
    dbms_output.put_line('The quotient of ' || l_grades ||' and ' || l_top_average || ' is ' || (l_grades / l_top_average)); 
  END IF;   
END; 
/

Table GRADES created.


1 row inserted.


1 row inserted.


1 row inserted.


1 row inserted.


1 row inserted.


STUDENT_ID SNAME                                                  GRADES TOP_AVERAGE
---------- -------------------------------------------------- ---------- -----------
      1001 Carmina                                                    75            
      1002 Danielle                                                   90         100
      1003 Sophia                                                     80         100
      1004 Zoe                                                        85         100
      1005 Carlo                                                      78         100

The sum of 75 and 100 is 175
The difference of 75 and 100 is -25
The product of 75 and 100 is 7500
The quotient of 75 and 100 is .75


PL/SQL procedure successfully completed.