pl-sql different programs

21
Database Management System Name:- Raj Upadhyay :- Dhruvan Bhalara

Upload: raj-upadhyay

Post on 19-Jan-2017

223 views

Category:

Education


0 download

TRANSCRIPT

Page 1: PL-SQL DIFFERENT PROGRAMS

Database Management System

Name:- Raj Upadhyay :- Dhruvan Bhalara

Page 2: PL-SQL DIFFERENT PROGRAMS

PL-SQL

Page 3: PL-SQL DIFFERENT PROGRAMS

PL/SQL (procedural language extension to Structured Query Language) definition. In Oracle database management, PL/SQL is a procedural language extension to Structured Query Language (SQL). The purpose of PL/SQL is to combine database language and procedural programming language.

Page 4: PL-SQL DIFFERENT PROGRAMS

PL-SQL DIFFERENT PROGRAMERS

Page 5: PL-SQL DIFFERENT PROGRAMS

The 'Hello World' Example:

1)

DECLARE message varchar2(20):= 'Hello, World!';BEGIN dbms_output.put_line(message);END;/

Page 6: PL-SQL DIFFERENT PROGRAMS

2)

BEGIN dbms_output.put_line('Hello, World!');END;/

Page 7: PL-SQL DIFFERENT PROGRAMS

1)Syntax: If statement. if(condition)thenstatementelsestatementend if

IF STATEMENT

Page 8: PL-SQL DIFFERENT PROGRAMS

2)IF THEN ELSIF Statement

if condition_1 then statements_1elsif condition_2 then statements_2[ elsif condition_3 then statements_3]...[ else else statements]End if;

Page 9: PL-SQL DIFFERENT PROGRAMS

declare i number(5);begini:=&i;if(mod(i,2)=0)then dbms_output.put_line('I is Even');else dbms_output.put_line('I is Odd');end if;end;/

Given number is Odd or Even:

Page 10: PL-SQL DIFFERENT PROGRAMS

Greater among 3 numberset serveroutput on;

declarei number(2);j number(2);k number(2);

begini:=&i;j:=&j;k:=&k;

if(i>j and i>k)thendbms_output.put_line('I is Greater');

elsif(j>i and j>k)thendbms_output.put_line('J is Greater');

else dbms_output.put_line('K is Greater');

end if;end;

Page 11: PL-SQL DIFFERENT PROGRAMS

set serveroutput on;

declarei number(5);j number(2);k number(2);

begin

i:=&i;

j:=((i/30));k:=(mod(i,30));

dbms_output.put_line(j||' Months '||k||' Days');

end;

Convert Days into Months and Remaining days

Page 12: PL-SQL DIFFERENT PROGRAMS

Compare two number using function• set serveroutput on;

• declare• i number(2);• j number(2);• k number(2);• max1 number(2);

• Function findmax(x in number, y in number, z in number)

• return number• is • w number;• begin• if(x>y and x>z)then• w:=x;• elsif(y>x and y>z)then• w:=y;• else • w:=z;• end if;• return w;• end;

• begin

• i:=&i;• j:=&j;• k:=&k;

• max1:=findmax(i, j, k);• dbms_output.put_line(max1);• end;

Page 13: PL-SQL DIFFERENT PROGRAMS

• DECLARE• a number;• b number;• c number;• FUNCTION findMax1(x IN number, y IN number)• RETURN NUMBER• IS • W NUMBER;• BEGIN• IF (x > y) THEN• dbms_output.put_line(x );• ELSIF(y>x)THEN• dbms_output.put_line(y);• ElSIF(y=x)THEN• dbms_output.put_line('x=y');• ELSE• dbms_output.put_line(' ');• END IF;• RETURN W;• END;

• BEGIN• a:= &a;• b:= &b;• c:=findMax1(a, b);• END;• /

Compare two number using function

Page 14: PL-SQL DIFFERENT PROGRAMS

Print 1 to 10 Using Simple Loopset serveroutput on;

declare a number(2);

begina:=1;

loopdbms_output.put_line(a);a:=a+1;if(a>10) thenexit;end if;end loop;end;

Page 15: PL-SQL DIFFERENT PROGRAMS

Print 1 to 10 Using FOR Loop

set serveroutput on;

declare a number(2);

begina:=1;for a in 1..10 loopdbms_output.put_line(a);end loop;end;

Page 16: PL-SQL DIFFERENT PROGRAMS

Print 1 to 10 Using While Loop

set serveroutput on;

declare a number(2);

begina:=1;while(a<11) loopdbms_output.put_line(a);a:=a+1;

end loop;end;

Page 17: PL-SQL DIFFERENT PROGRAMS

Print 1 to 10 Using Nested FOR Loopset serveroutput on;

declare a number(2);b number(2);

begina:=1;b:=1;<<outer>>for a in 1..10 loop

dbms_output.put_line(a);<<inner1>>

for b in 1..10 loopdbms_output.put_line(b);

end loop inner1;end loop outer;

end;

Page 18: PL-SQL DIFFERENT PROGRAMS

Sum of numbers from 1 to 50 (Using FOR Loop)

set serveroutput on;

declare a number(5);b number(5);

begina:=1;b:=0;for a in 1..50 loopb:=a+b;end loop;dbms_output.put_line(b);end;

Page 19: PL-SQL DIFFERENT PROGRAMS

Sum of numbers from 1 to 50 (Using Simple Loop)

set serveroutput on;

declare a number(5);b number(5);

begina:=0;b:=0;

loopa:=a+1;b:=a+b;if(a>49) thenexit;end if;end loop;dbms_output.put_line(b);end;

Page 20: PL-SQL DIFFERENT PROGRAMS

Sum of numbers from 1 to 50 (Using While Loop)

set serveroutput on;

declare a number(5);b number(5);

begina:=1;b:=a;while(a<50) loopa:=a+1;b:=a+b;end loop;dbms_output.put_line(b);end;

Page 21: PL-SQL DIFFERENT PROGRAMS

Thank You