PL/SQL中使用select语句,必须返回有且只有一条记录;
into关键字:ename的值放进v_ename中,sal的值放入v_sal中;
如果不能保证查询后有记录,则会报错;
如果查询出多条记录,也会报错.
小例子
declare v_emp emp%rowtype; begin select * into v_emp from emp where empno = 7369; dbms_output.put_line(v_emp.ename); end;
显示:Smith
这里采用“record”类型,相当于“v_emp”能存储“emp表”的一条记录。
PL/SQL的insert语句
declare v_deptno dept.deptno%type := 26; v-dname dept.dname%type := "lulu"; v_location dept.location%type := "beijing"; begin insert into dept2 values(v_deptno, v-dname, v_location); commit; end;
PL/SQL的update语句
declare v_deptno dept.deptno%type :=50; v_count number; begin update emp2 set sal= sal/2 where deptno = v_deptno; dbms_output.put_line(sql%rowcount); --表示更新语句影响了多少条记录 commit; end;
select deptno into v_deptno from emp2 where empno = 7369;影响了一条记录
select count(*) into v_count from emp2;也只影响了一条记录
PL/SQL执行DDL语句
需要加上execute immediate ‘.....’;
begin execute immediate ‘create table T (name varchar2(10) default ''lulu'')’; end;