標籤:cas 功能 for 迴圈 bms char ade pos 大於等於 acl
PL輸出語句
set serverout on; -- 開啟PL的輸出語句功能
declare
n number:=1; -- 聲明一個number型的變數n,並賦值為1
v varchar2(20):=‘world‘; -- 聲明一個varchar2類型的變數v 賦值為“world”
begin -- 控制語句書寫在 begin -- end 之間
dbms_output.put_line(‘hello‘||n||v); -- 輸出語句,資料連線使用‘||’
end;
if條件陳述式
set serverout on;
declare emp_count number;
begin
select count(*) into emp_count from emp where sal>=3000;
if emp_count>0 then
dbms_output.put_line(‘有‘||emp_count||‘個員工的基本薪資大於等於3000‘);
else
dbms_output.put_line(‘沒有員工的基本薪資大於等於3000‘);
end if;
end;
指派陳述式
set serverout on;
declare emp_count number;
begin
select count(*) into emp_count from emp where sal>=3000; -- 查詢出資料並賦值給 emp_count
if emp_count=1 then
dbms_output.put_line(‘有1個員工的基本薪資大於等於3000‘);
else if emp_count>1 then
dbms_output.put_line(‘有超過1個員工的基本薪資大於等於3000‘);
else
dbms_output.put_line(‘沒有員工的基本薪資大於等於3000‘);
end if;
end if;
end;
case when 流程式控制制語句
set serverout on;
declare emp_count number;
begin
select count(*) into emp_count from emp where sal>=3000;
case emp_count
when 0 then dbms_output.put_line(‘沒有員工的基本薪資大於等於3000‘);
when 1 then dbms_output.put_line(‘有1個員工的基本薪資大於等於3000‘);
when 2 then dbms_output.put_line(‘有2個員工的基本薪資大於等於3000‘);
when 3 then dbms_output.put_line(‘有3個員工的基本薪資大於等於3000‘);
else dbms_output.put_line(‘超過3個員工的基本薪資大於等於3000‘);
end case;
end;
無條件迴圈 loop
set serverout on;
declare g_id number:=2;
g_losal number;
g_hisal number;
begin
loop
if(g_id>4) then
exit;
end if;
select losal,hisal into g_losal,g_hisal from salgrade where grade=g_id;
dbms_output.put_line(g_id || ‘等級的最低薪資‘|| g_losal || ‘,最高薪資:‘ || g_hisal);
g_id:=g_id+1;
end loop;
end;
while 迴圈
set serverout on;
declare g_id number:=2;
g_losal number;
g_hisal number;
begin
while g_id<5 loop
select losal,hisal into g_losal,g_hisal from salgrade where grade=g_id;
dbms_output.put_line(g_id || ‘等級的最低薪資‘|| g_losal || ‘,最高薪資:‘ || g_hisal);
g_id:=g_id+1;
end loop;
end;
for 迴圈
set serverout on;
declare g_losal number;
g_hisal number;
begin
for g_id in 2..4 loop
select losal,hisal into g_losal,g_hisal from salgrade where grade=g_id;
dbms_output.put_line(g_id || ‘等級的最低薪資‘|| g_losal || ‘,最高薪資:‘ || g_hisal);
end loop;
end;
oracle 控制語句