PLSQL程式設計

2021-08-08 06:13:32 字數 2368 閱讀 6977

create table emomy1(emon varchar2(100),time1 date);

drop table emomy1;

select * from emomy1;

begin

dbms_output.put_line('hello,world');

end;

--定義變數

declare

--name constant varchar2(20) :='josn'; 賦予初始值,並且不能更改(constant);

name varchar2(20);

age number;

num number;

emomy varchar2(100);

--plsql語句

begin

name :='mike';

age :=12;

num :=10/0;

dbms_output.put_line(name||age);

--異常處理

exception

when others then

emomy :='除數不能為零';

dbms_output.put_line(name||',你算錯了');

insert into emomy1 values(emomy,sysdate);

end;

--賦值

declare

vsal scott.emp.sal%type;

begin

select sal into vsal from scott.emp where ename='king';

dbms_output.put_line(vsal);

end;

--控制流程  

begin

for i in 1..20 loop

if mod(i,2)=0 then

dbms_output.put_line(i);

else

dbms_output.put_line(i+1);  

end if;

end loop;

end;

declare

sqla varchar2(50);

begin

sqla :='&sql';

execute immediate sqla;

end;

select * from emp2;

--動態sql語句

declare

name varchar2(50);

sal number;

begin

-- 字串

name := upper('&ename');

-- 動態執行

execute immediate 'select sal+nvl(comm,0) from emp2 where ename =''' || name || '''' into sal;

dbms_output.put_line(name || '的工資是' || sal);

end;

--異常處理

declare

myinput number;

-- 宣告異常

myexception exception;

begin

myinput := &mynum;

if myinput > 10 then

dbms_output.put_line('ok');

elsif myinput >= 0 then

-- 主動丟擲異常

raise myexception;

else

-- 主動丟擲異常, 是 raise 語句的封裝

end if;

exception

-- 捕獲異常

when myexception then

dbms_output.put_line('數字太小');

end;

-- 儲存過程的引數型別

create or replace procedure p***  (name in varchar2,  -- 不能指定長度

s out number, c out number)

isbegin

select sal, nvl(comm, 0) into s, c from emp2 where ename = name;

end;

-- 呼叫過程

declare

s number;

c number;

begin 

p***('king', s, c);

dbms_output.put_line(s || '----' || c);

end;

PL SQL程式設計

1.具有程式語言的特點,他能把一組sql語句放到乙個模組中,使去更具有模組的程式的特點 2.採用過程性語言控制程式的結構,也就是說,在pl sql中增強邏輯結構,如迴圈,判斷等程式結構 3.pl sql可以對程式中的錯誤進行自動處理,使程式能夠在遇到錯誤時不會中斷,及他的處理異常機制 4.具有更好的...

PL SQL 程式設計

pl sql 程式結構 declare 宣告部分 宣告變數 型別 游標以及布局的儲存過程及函式 begin 執行部分 過程及sql語句,程式的主要部分,是必須的 end eg 基表為course declare v cno varchar 20 001 begin select from cours...

PL SQL程式設計基礎 PL SQL簡介

課程教師 李興華 課程學習者 陽光羅諾 日期 2018 07 28 知識點 1 了解pl sql的主要特點 2 掌握pl sql塊的基本結構 pl sql語法結構 語法 declare 宣告部分,例如。定義變數 常量 游標。begin 程式編寫 sql語句 exeception 處理異常 end 說...