PL/SQL 中使用 DML

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;

更多相關知識請戳主頁哦,啾咪,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章