oracle 控制語句

來源:互聯網
上載者:User

標籤: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 控制語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.