PLSQL 中使用 DML
select
查询语句,必须使用 into 关键字返回结果必须是一条,返回多条记录会报too_many_rows 异常,没有数据返回 no_data_found 异常。
declare
v_name emp.ename%type;
begin
select ename into v_name from emp where empno=&no;
dbms_output.put_line(v_name(0));
end;
insert、update 、delete
create table test_emp (empno number(5) ,ename varchar2(50),job varchar(20));
-- insert 新增
begin
insert into test_emp(empno,ename,job) values(1234,'xiaohua','mishu');
commit;
end;
---- 通过参数输入值 添加表数据
begin
insert into test_emp(empno,ename,job) values(&no,'&name','&job');
commit;
end;
-- 修改 update
begin
update test_emp set job='baobiao' where empno=2345;
commit;
end;
--delete 删除
begin
delete from test_emp where empno=&no;
commit;
end;
注:增删改注意提交事务
获得操作的记录条数
sql%rowcount 是游标中的一个属性,用来获得操作的记录数
declare
v_count number(10) ;
begin
delete from test_emp where empno=&no;
v_count := sql%rowcount; -- 获取操作记录数
commit;
dbms_output.put_line(v_count||'rows deleted');
end;
DDL
select count(*) from user_tables where table_name='TEST_EMP';
declare
v_count number(10);
begin
select count(*) into v_count from user_tables where table_name='TEST_EMP';
if v_count=1 then
execute immediate('drop table test_emp');
end if;
execute immediate('create table test_emp(id number(10) primary key , name varchar2(20))');
end;
更多相关知识请戳主页哦,啾咪,谢谢!