您的位置:首页 > 数据库

PLSql--乘法口诀表

2016-04-05 21:37 369 查看
上海汉得2016年校园招聘面试题

问题

在Oracle下实现乘法口诀表,包含存储到数据库、打印到屏幕等功能

解决

--> 乘法口诀表,创建表,结果插入表,打印

/*
create table t{
num1 number,
num2 number,
sum number,
}
*/

set serveroutput on

declare
i         number;
j         number;
temp      number;
resultion varchar2(20);
begin
for i in 1 .. 9 loop
for j in 1 .. i loop
temp      := i * j;
resultion := TO_CHAR(i) || '*' || TO_CHAR(j) || '=' || TO_CHAR(temp);
Multiply(i, j, temp);
dbms_output.put(resultion || '   ');
end loop;
dbms_output.put_line(' ');
end loop;
end;

/* Procedure */
create or replace procedure Multiply(i t.num1%type, j t.num2%type, r t.sum%type) IS
begin
insert into t values (i, j, r);
commit;
exception
when others then
rollback;
end Multiply;


结果



select * from t;




分析

数据库表的创建(储存三个字段,分别是乘数/被乘数/积)

循坏语句

函数使用 TO_CHAR

SQL语句DML

逻辑严谨性(exception)

说明

t.sum%type 是指此类型为数据库表t的sum字段类型

dbms_output.putline()函数是会换行的,而dbms_output.put()不换行。

字符串连接符‘||’是在oracle数据库下使用,MySQL/SQL Server使用‘+‘
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息