Oracle从小白到精通第八天(plsql基本语法,记录类型)(尚硅谷学习Oracle笔记)

1.plsql块的构成

在这里插入图片描述
输出一个helloworld
首先要在sql中 set serveroutput on
begin
dbms_output.put_line(‘helloworld’);
end;
命名规则
在这里插入图片描述
–查询100号员工的工资
declare
     --声明变量
     v_sal number(10,2);
     v_email varchar2(20);
     v_hire_date date;
begin
     --sql语句的操作:select。。。into。。from。。where
     select salary,email,hire_date into v_sal,v_email,v_hire_date
     from employees where employee_id=100;
     --打印
     dbms_output.put_line(v_sal||’,’||v_email||’.’||v_hire_date);
end;
into前后要一一对应,声明的变量范围不能小
其中可以动态的获取表中的数据类型:v_sal employees.salary%type;

2. 记录类型

记录类型是把 逻辑相关 的数据作为一个单元存储起来 ,称作 PL/SQL RECORD 的域(FIELD) ,其作用是存
放互不相同但逻辑相关的信息。
在这里插入图片描述

declare
     –声明一个记录类型
     type emp_record is record(
     v_sal employees.salary%type ,
     v_email employees.email%type ,
     v_hire_date date
     );
     –定义一个记录类型的成员变量
     v_emp_record emp_record ;
begin
     –sql语句的操作:select。。。into。。from。。where
     select salary,email,hire_date into v_emp_record
     from employees where employee_id=100;
     –打印
     dbms_output.put_line(v_emp_record.v_sal||’,’||v_emp_record.v_email||’,’||v_emp_record.v_hire_date);
end;

plsql中赋值的表达式时 := v_salary number(10,2) := 0;

使用 %rowtype
这样就能把整个表的列定义了
declare
     --声明一个记录类型的变量
     v_emp_record employees%rowtype;
begin
     --通过 select … into … 语句为变量赋值
     select * into v_emp_record
     from employees
     where employee_id = 186;

     – 打印变量的值
     dbms_output.put_line(v_emp_record.last_name || ', ’ || v_emp_record.email || ', ’ ||
          v_emp_record.salary || ', ’ || v_emp_record.job_id || ', ’ ||
          v_emp_record.hire_date);
end;

通过变量实现DELETE、INSERT、UPDATE等操作

declare
v_emp_id employees.employee_id%type;

begin
v_emp_id := 109;
delete from employees
where employee_id = v_emp_id;
–commit;
end;

3.plsql的知识框架

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值