



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
plsql - plsql
Typology: Lecture notes
1 / 27
This page cannot be seen from the preview
Don't miss anything!
Data types in PL/SQL: The data types which are using in SQL same data types are supported in PL/SQL. Operators in PL/SQL: The operators which are using in SQL same operators are by PL/SQL except assignment operator. Input statements in PL/SQL:
Block: Block is grouped code or set of statements, it is classified into two types.
b:=a-b; a:=a-b; dbms_output.put_line('value of a is'||a); dbms_output.put_line('Value of b is'||b); end; P) Write a PL/SQL block to converts feets into inches. declare a number:=&a; begin dbms_output.put_line('enter feet values'||a); a:=a*12; dbms_output.put_line('inches for give value'||a); end; Conditional statements in PL/SQL: Syntax: If(condition) then Statement 1; Statemnet 2;
Statement n: Else if (Condition 1) Statement3; Statement4; else statement n; End if;
This is the conditional statement in the PL/SQL. Which checks the condition either true or false by means of relational operator such as >, <, >=, <= et.c., If the condition is satisfied it executes the statement between if and else. If the condition is not satisfied then it will execute the statements after else. Note: Every if condition should ends with end if statemet..
P) Write a PL/SQL block input two numbers and find biggest one. declare a number:=&a; b number:=&b; begin if(a>b) then dbms_output.put_line('a is big'); elsif(a=b) then dbms_output.put_line('both are same'); else dbms_output.put_line('b is big'); end if; end;
P) Write a PL/SQL block input any positive number and check it out even or odd? declare a number:=&a; begin if(a =0) then dbms_output.put_line(a||'is neither even nor odd');
nb number; begin if(tc='d') then nb:=cb+tamt; dbms_output.put_line('The net balance is' ||nb); else if(tc='w') then nb:=cb-tamt; dbms_output.put_line('The net balance is' ||nb); else dbms_output.put_line('Please enter d or w'); end if; end if; end;
P) Write Pl/SQL block input consumer number, Consumer name, and starting reading number ending reading number, slap type (i-industry, c-commercial, r-residence). Calculate units consumed? Conditions:
declare cno number(10):=&no; cname varchar2(10):='&cname';
stno number(10):=&stno; edno number(10):=&edno; slab varchar2(1):='&slab'; tu number; begin tu:=edno+stno; if(slab='i') then tu:=tu5; dbms_output.put_line('total unit cost is'||tu); elsif(slab='c') then tu:=tu4; dbms_output.put_line('total unit cost is'||tu); elsif(slab='r') then tu:=tu*3; dbms_output.put_line('total unit cost is'||tu); else dbms_output.put_line('Please enter i or c or r'); end if; end; (OR) declare cno number(10):=&no; cname varchar2(10):='&cname'; stno number(10):=&stno; edno number(10):=&edno; slab varchar2(1):='&slab';
If slab type is residence and units consumed <=100 then unit rate is 3/- else 4/- calculate total charge. declare
cno number(10):=&no; cname varchar2(10):='&cname'; stno number(10):=&stno; edno number(10):=&edno; slab varchar2(1):='&slab'; tu number; begin tu:=edno+stno; if(slab='i') then if(tu<=100) then tu:=tu5; dbms_output.put_line('total unit cost is'||tu); else tu:=tu6; dbms_output.put_line('total unit cost is'||tu); end if; end if; if(slab='c') then if(tu<=100) then tu:=tu*4; dbms_output.put_line('total unit cost is'||tu); else
tu:=tu5; dbms_output.put_line('total unit cost is'||tu); end if; end if; if(slab='r') then if(tu<=100) then tu:=tu3; dbms_output.put_line('total unit cost is'||tu); else tu:=tu*4; dbms_output.put_line('total unit cost is'||tu); end if; end if; end; Loop:
Note: If i is participating in loop it is not possible.
P) To print 10 numbers on console. declare i number; begin for i in 1..10 loop dbms_output.put_line(i); end loop; end; P) To print 10 even numbers declare i number; begin for i in 1..5 loop dbms_output.put_line((i2)); end loop; end; P) To print 10 odd numbers declare i number; begin for i in 1..5 loop dbms_output.put_line((i2)-1); end loop; end; P) To find factorial number.
declare
i number; n number:=&n; f number:=1; begin for i in 1..n loop f:=f*i; end loop; dbms_output.put_line(f); end; P) Check it is prime or not. declare i number; n number:=&n; c number:=0; begin for i in 1..n loop if(mod(n,i)=0) then c:=c+1; end if; end loop; if(c=2) then dbms_output.put_line(n||'is prime'); else dbms_output.put_line(n|| 'is not prime'); end if;
n:=floor(n/10);
end loop; dbms_output.put_line(s); end;
for reverse:
set serveroutput on; declare a number:= 1; begin for i in reverse 1..5 loop dbms_output.put_line(i); end loop; end;
Loop Syntax: Syn: Loop Statement 1;. . Statement n; Exit when(condition) End loop;
Here the statements are executed as long as the given condition is not satisfied control automatically comes outer loop.
Write a PL/SQL block to print 10 times a String. declare i number:=1; begin loop dbms_output.put_line('Hello'); i:=i+1; exit when(i>10); end loop; end;
Write a PL/SQL block print the employee details such as employee name, salary and deptno when employee number passes as in value. declare a number; b varchar2(10); c number; d number; begin select ename,sal,deptno into b,c,d from emp where empno=&a; dbms_output.put_line(b||' ' ||c||' '||d);
P) Write a PL/SQL block to raise zero divide exception.
set serveroutput on; declare a number:=&a; b number:=&b; c number; begin c:=a/b; dbms_output.put_line('division is'||c); exception when zero_divide then dbms_output.put_line('second value cannot be zero'); end;
Oracle Exception Name Oracle Error
Explanation DUP_VAL_ON_INDEX ORA- 01
You tried to execute an INSERT or UPDATE statement that has created a duplicate value in a field restricted by a unique index. TIMEOUT_ON_RESOURCE ORA- 51
You were waiting for a resource and you timed out. TRANSACTION_BACKED_OUT ORA- 61
The remote portion of a transaction has rolled back. INVALID_CURSOR ORA- 01
You tried to reference a cursor that does not yet exist. This may have happened because you've executed a FETCH cursor or CLOSE cursor before OPENing the cursor.
You tried to execute a call to Oracle before logging in. LOGIN_DENIED ORA- 17
You tried to log into Oracle with an invalid username/password combination. NO_DATA_FOUND ORA- 03
You tried one of the following:
You tried to execute a SELECT INTO statement and more than one row was returned. ZERO_DIVIDE ORA- 76
You tried to divide a number by zero.
INVALID_NUMBER ORA- 22
You tried to execute a SQL statement that tried to convert a string to a number, but it was unsuccessful. STORAGE_ERROR ORA- 00
You ran out of memory or memory was corrupted. PROGRAM_ERROR ORA- 01
This is a generic "Contact Oracle support" message because an internal problem was encountered. VALUE_ERROR ORA- 02
You tried to perform an operation and there was a error on a conversion, truncation, or invalid constraining of numeric or character data. CURSOR_ALREADY_OPEN ORA- 11
You tried to open a cursor that is already open.