File size: 13,642 Bytes
a0d1dd2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
PL/SQL EXAMPLES
-- demonstration of anonymous block
DECLARE
v_empno emp.empno%type;
v_name emp.ename%type;
v_sal emp.sal%type;
v_real real := 1.234;
BEGIN
SELECT ename, sal
INTO v_name, v_sal
FROM emp
WHERE empno = &v_empno;
dbms_output.put_line('The salary of ' || v_name ||' is: USD '||to_char(v_sal));
dbms_output.put_line('The real is '||to_char(v_real));
EXCEPTION
when NO_DATA_FOUND then
dbms_output.put_line('Could not find any one!');
END;
/
------------------------------------------------------------------------------------------------------------------------------
-- example of implicit cursor: SQL%NOTFOUND
BEGIN
update emp
set sal = 5000
where empno = &my_empno;
IF SQL%NOTFOUND then
dbms_output.put_line('could not update anyone');
END IF;
END;
/
-----------------------------------------------------------------------------------------------------------------------------
create or replace function check_phone(phone varchar2) return number is
begin
-- remove all valid char in "1234567890-()", all invalid chars remained
if replace(translate(phone,' 1234567890()-',' '),' ') is not null then
return 0;
else
return 1;
end if;
end check_phone;
----------------------------------------------------------------------------------------------------------------------------------------
-- demonstration of function
create or replace FUNCTION square (in_value in number) RETURN number is
BEGIN
return (in_value*in_value);
END;
/
select sal, square(sal) from emp;
select square(5) from dual;
-------------------------------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION get_sal
(v_id IN emp.empno%TYPE)
RETURN NUMBER
IS
v_salary emp.sal%TYPE :=0;
BEGIN
SELECT sal
INTO v_salary
FROM emp
WHERE empno = v_id;
RETURN (v_salary);
END get_sal;
/
select empno, sal, get_sal(empno) from emp;
VARIABLE G_SAL NUMBER
EXECUTE :G_SAL := GET_SAL(7369);
PRINT :G_SAL;
-------------------------------------------------------------------------------------------------------------------------------------------
create or replace PROCEDURE TEST_INPUT (IN_NUM IN NUMBER) IS
V_NUMBER NUMBER := 5;
V_CHAR VARCHAR2(20);
BAD_DATA EXCEPTION;
BEGIN
IF (IN_NUM < 10) THEN
V_CHAR := 'SMALL NUMBER';
ELSIF (IN_NUM >= 10 AND IN_NUM < 20) THEN
V_CHAR := 'GOOD NUMBER';
ELSE
V_CHAR := 'BAD DATA FOUND';
raise BAD_DATA;
END IF;
dbms_output.put_line(v_char);
EXCEPTION
WHEN BAD_DATA THEN
dbms_output.put_line('error entered number');
END;
/
------------------------------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE PROCEDURE query_emp (v_id IN emp.empno%TYPE,
v_name OUT emp.ename%TYPE,
v_salary OUT emp.sal%TYPE,
v_comm OUT emp.comm%TYPE)
IS
BEGIN
SELECT ename, sal, comm
INTO v_name, v_salary, v_comm
FROM emp
WHERE empno = v_id;
END query_emp;
/
VARIABLE g_name varchar2(15)
VARIABLE g_salary number
VARIABLE g_comm number
EXECUTE query_emp (7654, :g_name, :g_salary,:g_comm);
print :g_name;
-----------------------------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE PROCEDURE format_phone (v_phone_no IN OUT VARCHAR2)
IS
BEGIN
v_phone_no := '(' || SUBSTR(v_phone_no,1,3) ||
')' || SUBSTR(v_phone_no,4,3) ||
'-' || SUBSTR(v_phone_no,7);
END format_phone;
/
VARIABLE g_phone_no varchar2(15)
BEGIN :g_phone_no := '8006330575'; END;
EXECUTE format_phone (:g_phone_no)
----------------------------------------------------------------------------------------------------------------------------------------
create or replace PROCEDURE sql_example(empno_num number) IS
my_empno emp.empno%type;
my_ename emp.ename%type;
BEGIN
DELETE FROM emp
WHERE empno = empno_num;
INSERT INTO emp (empno, ename)
VALUES (1234,'Tran Hung');
SELECT empno, ename INTO my_empno, my_ename
FROM emp
WHERE hiredate = to_date( '01-JAN-81') ;
EXCEPTION
when DUP_VAL_ON_INDEX then
dbms_output.put_line('duplicated');
when TOO_MANY_ROWS then
dbms_output.put_line('more than one person in 81');
when NO_DATA_FOUND then
dbms_output.put_line('No data found');
END;
-----------------------------------------------------------------------------------------------------------------------------------------------
create or replace procedure test_double_block is
e_mess char(80);
BEGIN
DECLARE
n1 number(4);
BEGIN
select empno
into n1
from emp
--where job = 'TTT';
where job = 'CLERK';
EXCEPTION
when TOO_MANY_ROWS then
dbms_output.put_line('more than one CLERK person ');
END;
DECLARE
n2 number(4);
BEGIN
select empno
into n2
from emp
where job = 'MANAGER';
EXCEPTION
when TOO_MANY_ROWS then
dbms_output.put_line('more than one MANAGER person ');
END;
EXCEPTION
when OTHERS then
e_mess := SUBSTR(SQLERRM,1,80);
dbms_output.put_line('e_mess: ' ||e_mess);
dbms_output.put_line('e_mess: ' ||SQLERRM);
END;
/
------------------------------------------------------------------------------------------------------------------------------------------------------
create PROCEDURE change_sal(p_per_1 NUMBER,p_per_2 NUMBER, p_deptno NUMBER) IS
CURSOR c_emp IS select empno,sal from emp where deptno=p_deptno;
r_emp c_emp%rowtype;
v_sal NUMBER;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO r_emp;
EXIT WHEN c_emp%NOTFOUND ;
IF r_emp.sal >1000 THEN
v_sal:=r_emp.sal*p_per_2;
ELSE
v_sal:=r_emp.sal*p_per_1;
END IF;
UPDATE emp
SET sal=v_sal
WHERE empno=r_emp.empno;
END LOOP;
CLOSE c_emp;
COMMIT;
END;
------------------------------------------------------------------------------------------------------------------------------------------------------
create or replace PROCEDURE cursor_test (numRow number) is
cursor emp_cursor is
select * from emp
for update;
BEGIN
dbms_output.put_line('ename'||' '||'empno'||' '||'sal');
for myEmp in emp_cursor loop
exit when emp_cursor%ROWCOUNT > numRow or emp_cursor%notfound;
dbms_output.put_line (myEmp.ename||' '||to_char(myEmp.empno)
|| ' '||to_char(myEmp.sal));
update emp
set sal = 5000
where current of emp_cursor;
end loop;
commit;
END;
/
----------------------------------------------------------------------------------------------------------------------------------------------------------
create or replace procedure test_cursor_for is
BEGIN
dbms_output.put_line ('empno'||' '||'ename');
FOR r_emp IN (select empno, ename from emp where job = 'CLERK')
LOOP
dbms_output.put_line (to_char(r_emp.empno)||' '||r_emp.ename);
END LOOP;
END;
----------------------------------------------------------------------------------------------------------------------------------------------------------
-- demonstration of EXCEPTION_INIT PRAGMA
DECLARE
null_data EXCEPTION;
PRAGMA EXCEPTION_INIT (null_data, -01400);
BEGIN
insert into emp (empno, ename)
values (null,'ttt');
EXCEPTION
when null_data then
dbms_output.put_line('null data found');
END;
/
-------------------------------------------------------------------------------------------------------------------------------------------------------
DECLARE
My_empno number;
keep_empno number;
BEGIN
My_empno := 59694;
SELECT empno INTO keep_empno
FROM emp
WHERE empno = my_empno;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No Data Found');
END;
/
-------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE PACKAGE manage_emp IS
PROCEDURE add_employee
(v_empno NUMBER
, v_ename varchar2
,v_job varchar2);
PROCEDURE rem_employee (v_empno NUMBER);
END manage_emp;
/
CREATE OR REPLACE PACKAGE BODY manage_emp IS
PROCEDURE add_employee
(v_empno NUMBER
, v_ename varchar2
,v_job varchar2) IS
BEGIN
INSERT INTO EMP (empno, ename, job)
VALUES (v_empno, v_ename, v_job);
END add_employee;
PROCEDURE rem_employee (v_empno NUMBER) IS
BEGIN
DELETE FROM emp
WHERE empno = v_empno;
END rem_employee;
END manage_emp;
/
EXECUTE MANAGE_EMP.ADD_EMPLOYEE(7899,'TRAN MINH','SALESMAN');
EXECUTE MANAGE_EMP.REM_EMPLOYEE(7876);
--------------------------------------------------------------------------------------------------------------------------------------------------------
-- CREATE TRIGGER: EXAMPLE
CREATE OR REPLACE TRIGGER insert_on_sunday
BEFORE INSERT ON emp
BEGIN
IF TO_CHAR(sysdate,'DY') in ('SAT', 'SUN') then
dbms_output.put_line('OK, you can INSERT');
ELSE
dbms_output.put_line('You can not INSERT');
RAISE_APPLICATION_ERROR (-20500, 'You may only insert into EMP during normal hours.');
END IF;
END;
-------------------------------------------------------------------------------------------------------------------------------------------------------
PL/SQL EXERCISESE,
*** PL/SQL subprograms 1
create or replace PROCEDURE change_emp_sal is
cursor emp_cursor is
select * from emp
for update;
BEGIN
for myEmp in emp_cursor loop
exit when emp_cursor%notfound;
if myEmp.sal < 1000 then
update emp
set sal = sal*1.05
where current of emp_cursor;
else
update emp
set sal = sal*1.15
where current of emp_cursor;
end if;
end loop;
commit;
END;
/
--------------------------------------------------------------------------------------------------------------------------------------------------------
*** PL/SQL subprogram: 2
create or replace PROCEDURE five_high_sal IS
cursor c_five_sal is
select empno, ename, sal from emp e
where 5 > (select count(*) from emp where e.sal < sal);
r_five_sal c_five_sal%rowtype;
BEGIN
dbms_output.put_line(' ');
dbms_output.put_line('empno'||' '||'ename'||' '||'sal');
OPEN c_five_sal;
LOOP
FETCH c_five_sal INTO r_five_sal;
EXIT when c_five_sal%NOTFOUND;
dbms_output.put_line(to_char(r_five_sal.empno)||' ' || r_five_sal.ename||' ' ||to_char( r_five_sal.sal));
END LOOP;
CLOSE c_five_sal;
EXCEPTION
when NO_DATA_FOUND then
dbms_output.put_line('No data found');
when OTHERS then
dbms_output.put_line('other error:'||SQLERRM);
END;
/
-------------------------------------------------------------------------------------------------------------------------------------------------------
**** PL/SQL subprograms: 3
create or replace PROCEDURE sql_update(empno_num number) IS
my_sal emp.sal%type := 6000;
update_sig EXCEPTION;
BEGIN
update emp
set sal = my_sal
where empno = empno_num;
if SQL%NOTFOUND then
raise update_sig;
end if;
EXCEPTION
when update_sig then
dbms_output.put_line('zero row updated');
when NO_DATA_FOUND then
dbms_output.put_line('No data found');
when OTHERS then
dbms_output.put_line('other error:'||SQLERRM);
END;
/
-------------------------------------------------------------------------------------------------------------------------------------------------------
select user, sysdate, rowid, rownum from sys.dual;
-------------------------------------------------------------------------------------------------
PL/SQL Tables:
PL/SQL tables use a primary key to give you array-like access to rows.
Like the size of a database table, size of a PL/SQL table is unconstrained. That is, the number of rows in a PL/SQL table can
increase dynamicalyy. So, your PL/SQL table grows as new rows are added.
PL/SQL tables can have one column and a primary key, neither of which can be named. The column can belong to any
scalar type, but the primary key must belong to type BINARY_INTEGER.
DECLARE
TYPE enameTabTyp IS TABLE OF char(10)
INDEX BY BINARY_INTEGER;
----------------------------------------------------------------------------------------------------
select to_char(hiredate,'YY') year, count(empno) number_of_emps
from emp
group by to_char(hiredate,'YY')
having count(empno) = (select max(count(empno)) from emp group by to_char(hiredate,'YY'))
/
____________________________________________________________________________________________
Packaged Cursors: You can separate a cursor specification from its body for placement in a package by
using the RETURN clause, as the following example:
CREATE PACKAGE EMP_ACTIONS AS
/* DECLARE CURSOR SPECIFICATION */
CURSOR C1 RETURN EMP%ROWTYPE;
END EMP_ACTIONS;
CREATE PACKAGE BODY emp_actions as
/* define cursor body */
CURSOR c1 RETURN emp%ROWTYPE
SELECT * FROM emp WHERE sal > 3000;
...
END emp_actions;
That way, you can change the cursor body without changing the cursor specification.
A cursor specification has no SELECT statement because the RETURN clause defines the datatype of the
result value. You can use the %ROWTYPE attribute in a RETURN clause to provide a record type
that represents a row in a database table. |