您的位置:首页 > 数据库 > Oracle

oracle数据库:PL/SQL 程序基础

2020-07-28 12:16 851 查看

1、使用dbms_output.put_line函数输出变量的值

declare
numm number := 5;
begin
dbms_output.put_line(numm);
end;

2、编写一个匿名程序块,来计算一个整数是不是偶数

declare
numm number := 4324322;
begin
if mod(numm, 2) = 0 then
dbms_output.put_line('Yes');
else
dbms_output.put_line('No');
end if;
end;

3、编写一个匿名程序块,来计算一个整数是不是素数

declare
num int:=2;
sqr int;
isprime boolean:=true;
begin
sqr:=sqrt(num);
for i in 2..sqr
loop
if mod(num,i)=0
then
isprime:=false;
exit;
end if;
end loop;

if isprime
then
dbms_output.put_line('YES');
else
dbms_output.put_line('NO');
end if;
end;

4、定义一个绑定变量,用于接收一个整数n,程序计算这个数的数位和

accept num prompt '请输入一个数字';
declare
--定义变量保存用户从键盘输入的数字
pnum int:= # --&地址符号,将num地址上的值赋给pnum
zz int;
p1 int;
begin
zz:=0;
while pnum>0 loop
p1:=mod(pnum,10);
pnum:=pnum/10;
zz:=zz+p1;
end loop;
dbms_output.put_line(zz);
end;

5、定义一个替换变量,用于接收一个字符串,字符串的每个子项为一个字符串,之间用逗号隔开。程序用于提取每个子串,并每行输出一个子串。

accept num prompt '请输入一个字符串';
declare
pnum varchar(100):=# --&地址符号,将num地址上的值赋给pnum
zz int;
p1 number;
str varchar(100);
begin
zz:=length(pnum);
while instr(pnum,',')>0
loop
p1:=instr(pnum,',');
str:=SUBSTR(pnum,1,p1-1);
--dbms_output.put_line(pnum);
dbms_output.put_line(str);
pnum:=substr(pnum,p1+1,zz);
--dbms_output.put_line(pnum);
zz:=zz-p1-1;
end loop;
dbms_output.put_line(pnum);
end;

6、编写一个嵌套的程序块,在内部块中使用外部块的同名变量

accept str prompt '请输入一个字符串';
declare
v_out varchar2(40);
begin
declare
begin
v_out:=to_char('&str');
dbms_output.put_line('使用外部块的同名变量:' || v_out);    end;
end;

7、编写一个匿名程序块,用于读取employees表中的某员工的first_name,last_name,显示该员工的first_name和last_name

accept str prompt '请输入员工id';
declare
v number;
first_name EMPLOYEES.FIRST_NAME%type;
last_name EMPLOYEES.last_name%type;
begin
v:=&str;
select first_name into first_name from employees where employee_id=v;
select last_name into last_name from employees where employee_id=v;
dbms_output.put_line(first_name);
dbms_output.put_line(last_name);
end;

8、编写一个匿名程序块,其中使用update语句employees表中某些员工的工资

begin
update employees set salary=300000 where employee_id=100;
end;
commit;

9、编写一个匿名程序块,向employees表中插入一行记录

begin
INSERT INTO "HR"."EMPLOYEES" VALUES (
'99', 'Sttten', 'Kitng', 'SKttING',
'525.123.4567',
TO_DATE('1986-06-17 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'),
'AD_VP', '300000', NULL, 100, '90');
end;
commit;

10、编写一个匿名程序块,删除employees表中的一行记录

begin
delete from employees where employee_id=99;
end;

11、使用隐式游标输出上一条sql语句影响的行数

declare n number;
begin
  delete from employees where employee_id=99;
  n:=sql%rowcount;
  commit;
  dbms_output.put_line(n);
end;

12、使用%rowtype类型定义变量,读取employees表的一行,并显示其对应的数据

declare
yihang employees%rowtype;
begin
select * into yihang from employees where employee_id=101;
dbms_output.put_line(yihang.first_name);
dbms_output.put_line(yihang.last_name);
end;

13、使用%rowtype类型定义变量,读取employees表的一行,并更改这个变量对应的域后,插入employees表和更新employees表对应的记录

declare
yihang employees%rowtype;
zz number;
begin
select * into yihang from employees where employee_id=100;
yihang.employee_id:=98;
yihang.first_name:='fdsfa';
yihang.salary:=32323;
yihang.job_id:='AD_VP';
yihang.manager_id:=100;
yihang.department_id:=90;
insert into employees values yihang;
end;
commit;

14、建立一个index-by表,读取employee_id在100和110之间员工记录到这个表。然后遍历这个index-by表,显示员工的信息。

create table index_by as select * from employees where 1=1 and employee_id  between 100 and 110;
select * from index_by;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: