【问题标题】: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?我正在尝试结合 PL/SQL 和 SQL 命令来操作数据库。我做得对吗?任何帮助和更正?
【发布时间】:2022-01-07 19:29:30
【问题描述】:

在此处输入图像描述使用带有 SQL 命令的 PL/SQL 操作数据库(PL/SQL 结构、数据类型、变量、DBMS 输出和条件),这是我的代码和我所理解的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; 
/

【问题讨论】:

  • 您已经为 MySQL 和 Oracle 这两个不同的数据库引擎标记了这个。我从错误中假设您实际上只使用 Oracle。请将代码发布为文本而不是图像。您发布的图片缺少与declare ... begin 对应的end;,但我不知道您是否已将其删除。
  • 将您的代码提供为可用于复制的格式正确的文本,而不是屏幕截图。为测试代码所需的所有表提供 CREATE TABLE(可能还有 INSERT INTO)代码。
  • 不要将代码添加为评论 - 编辑您发布的原始消息(问题)。

标签: sql oracle plsql oracle-sqldeveloper


【解决方案1】:

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

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”列或添加一个值。

但是,该问题与您遇到的错误无关。屏幕截图给出了它。您正在选择以下 3 行

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

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

解决它。将光标放在 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.

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
  • 2021-11-10
  • 1970-01-01
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多