Top Banner
User-defined Functions User-defined Functions CSED421: Database Systems Labs CSED421: Database Systems Labs
12

User-defined Functions CSED421: Database Systems Labs.

Jan 04, 2016

Download

Documents

Allan Austin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: User-defined Functions CSED421: Database Systems Labs.

User-defined FunctionsUser-defined Functions

CSED421: Database Systems LabsCSED421: Database Systems Labs

Page 2: User-defined Functions CSED421: Database Systems Labs.

User-Defined FunctionsUser-Defined Functions

To provide functionality that is not available in SQL or SQL built-in functions

PL/SQL (Procedural Language extension to SQL) Procedural language built in Oracle

Support conditional (IF) statement, loop (WHILE, FOR) statement

Procedure / Function

Page 3: User-defined Functions CSED421: Database Systems Labs.

User-Defined Functions (cont.)User-Defined Functions (cont.)

Can appear in a SQL statement anywhere SQL functions can appear Select list of a SELECT statement

Condition of a WHERE clause

ORDER BY, and GROUP BY clauses

VALUES clause of an INSERT statement

SET clause of an UPDATE statement

Page 4: User-defined Functions CSED421: Database Systems Labs.

RETURN ClauseRETURN Clause

Every function must return a value

Oracle SQL does not support calling of functions with boolean parameters of returns Design functions to return number (0 or 1) or strings (‘TRUE’ or

‘FALSE’)

The data type cannot specify a length, precision, or scale

Page 5: User-defined Functions CSED421: Database Systems Labs.

SyntaxSyntax

Page 6: User-defined Functions CSED421: Database Systems Labs.

ExamplesExamples

Variable declaration

PL/SQL Block

the same type as jobs.job_id

Page 7: User-defined Functions CSED421: Database Systems Labs.

ExamplesExamples

Convert Celsius to Fahrenheit CREATE OR REPLACE FUNCTION CtoF (Celsius IN NUMBER)

RETURN NUMBER ISBEGIN

RETURN (Celsius * 1.8) + 32;END;/

Page 8: User-defined Functions CSED421: Database Systems Labs.

ExamplesExamples

IF statements IF k=0 THEN

result:=-1;ELSIF k=1 OR k=2 THEN

result:=1;ELSE

result:=0;END IF;

WHILE statements WHILE k > 0 LOOP

k := k – 1;

END LOOP;

create or replace function make_zero (k in number)return number is result number; begin

result := k; while result > 0 loopresult := result - 1;

end loop; return result; end make_zero;/

Page 9: User-defined Functions CSED421: Database Systems Labs.

Practice 1Practice 1

“ORA-01476: divisor is equal to zero” 문제를 피해가기 위한 나누기 함수를 생성하고 확인 함수명은 divide 로 생성 함수는 두 개의 숫자 (NUMBER) a, b 를 입력 받는다 b=0 이면 결과로 0 을 RETURN 한다 b!=0 이면 a/b 를 RETURN 한다 확인하는 방법 : (1) temp variable 선언 , (2) execute 명령어를

사용하여 divide function 을 실행 , (3) print 명령어를 사용하여 출력

Page 10: User-defined Functions CSED421: Database Systems Labs.

Practice 2Practice 2

사원번호를 치면 department_name 을 출력하고 싶다 . 출력하는 함수를 만드시오 . 그리고 함수를 확인하기 위해 사원번호 , 이름 , 부서명을 출력하는 쿼리를 입력하시오 . 주의 : return 타입으로 varchar2 를 사용 , varchar2(20) 과

같이 scale 을 정의 불가

Page 11: User-defined Functions CSED421: Database Systems Labs.

Practice 3Practice 3

10 진수를 16 진수 값으로 CONVERSION 하는 함수를 만들고 , 함수를 확인하기 위해 사원번호를 16 진수로 출력하는 쿼리를 입력하시오 힌트 : 사용 가능한 built-in function

모듈로 연산 : 17 mod 16

버림 연산 : trunc(2.2)

숫자를 char 로 변환 : to_char(2)

Concatenation: concat(‘abc’, ‘d’)

주의 : return 타입으로 varchar2 를 사용

varchar2(20) 과 같이 scale 을 정의 불가

EMPLOYEE_ID DECTOHEX(EMPLOYEE_ID)-------------------------------------

100 64101 65102 66103 67104 68107 6B124 7C141 8D142 8E143 8F144 90149 95174 AE176 B0178 B2200 C8201 C9202 CA205 CD206 CE

20 rows selected.

Page 12: User-defined Functions CSED421: Database Systems Labs.

ReferencesReferences

Oracle Database Application Developer’s Guide - Fundamentals http://stanford.edu/dept/itss/docs/oracle/10g/appdev.101/b107

95/adfns_pc.htm#ADFNS009

Oracle Database SQL Reference – CREATE FUNCTION http://stanford.edu/dept/itss/docs/oracle/10g/server.101/b1075

9/statements_5009.htm#i2153260