Вы находитесь на странице: 1из 15

ASSIGNMENT ON DS/NC PROGRAMMING LAB

Submitted by:

NISHANTA RANJAN NANDA Roll No..: 112VMIT11012

Mtech-IT DEPARTMENT OF STASTICS UTKAL UNIVERSITY [2011 2012]

Assignment Example: Declare X number(10); Begin Select sal into x from emp where ename=test; Dbms-output. Put-line(x); End; Example: Begin Dbms-output.put-line(The time is:); End; SET SERVERR OUTPUT ON It is used to display the out put on out put screen.when all programs of pl/sql is correct but it does not get any output.then On the set server output Example: Begin Dbms-output.put_line(The time is:); Dbms-output.put-line(sysdate); End; If-then statement Syntax If condition then program statement End if; Example: Question:Find the lesser number between two number declare a number(b); b number(b); Begin A:=23; B:=a*5; If a<b then Dbms-output.put-line(a 11 is less than 11) End if; End; Ans:23 is less than 115

If-Else Statement Syntax If condition then if condition is true else if condition is false end if; Question: Find the number wheather it is lesser or greater between 2 number. Declare a number(b); b number(b); begin a:=23; b:=a/5; If a<b then Dbms------(a!! is less than!! b); Else Dbms-------(a!!is less thanb); End if End; Question: Find the maximum number between if statement nesting three number Declare A number(b); B number(b); C number(b); Res number(b); Begin A:=23; B:=a/5; C:=b*7; If a>b then If a>c then Res:=a; Else Res:=c; End if; Else If b>c then Res:=b; Else Res:=c;

End if; End if; Dbms-----(max of:!!a!!,!!b!!,and!!c!!is!!res); End; Question:Find the grade for a given mark if-else if statement Declare N grade number; L grade char(2); Begin N grade:=82.5; If n grade>95 then L grade:=At; Else if n grade>90 then Lgrade:=A; Else if ngrade>85 then Lgrade:=Bt; Else if ngrade>80 then Lgrade:=B; Else if ngrade>75 then Lgrade:=c+; Else if ngrade>70 then Lgrade:=c; Elseif ngrade >65 then Lgrade:=dt; Elseif ngrade>60 then Lgrade:=d; Else lgrade:=f; End if; Dbms-----(grade!!ngrade!!is!!lgrade); End; Ans:grade82.5 is B LOOP EXIT LOOP Syntax Loop statements If condition then Exit; Statements End loop; Question: Print the number from 1t0 5 using loop Example: declare T number(b); Begin

T:=1; Loop Dbms----(a!!!T); T:=T+1; If T>5 then Exit; End if; Dbms-----(b1:!!T); End loop; End; LOOP EXIT WHEN Syntax Loop Various statements exit when condition; Various statements End loop; Example: Declare T number(b); Begin T:=1; Loop Dbms-----(a1:!!T); T:=T+1; Exit when T>5; Dbms-----(b1:!!T); End loop; End; WHILE..LOOP Print the numbers from 1 to 5 using while loop Syntax: While condition Loop Various statements End loop; Example: declare T number(b); Begin T:=1; While T<=5 Loop

Dbms----(a1:!!T); T:T+1; Dbms-----(b1:!!T); End loop; End; FOR..LOOP Syntax For counter in startend Loop Various statements End loop; Example: Begin For T in 15 Loop Dbms-----(T:!!T); End loop; End; Or Print the number 2 to 7 using for loop Declare J number(b); K number(b); Begin J:=7; K:=2; Loop dbms-------(I:!!I); end loop; CASE STATEMENT Case statements also selects one sequence of statements to execute like an if. Using case statement makes the program more readable and efficient than length 4 ifthen statements write a program using case. Syntax Case<variable> <value1> then Statement When<value2>then

Statement Else Statement End case; Question:Enter the number 1to 6 and show corresponding days Day of a week; Declare Ch number(5):=&ch; Choice varchar2(20); Begin Choice:=case ch When 0 then sunday When 1 then monday When 2 then tuesday When 3 then thursday When 4 then friday When 5 then saturday Else wrong choice End; Dbms--------(the day is!!choice ) end; Question:sql program to find out the sum & product of two number. Declare A number:=&a; B number:=&b; S number; P number; Begin S:=a+b; Output.put-line(sum is=!!s); Output.put-line(product:=!!s); End; If multiplication table of n number; Declare

P number; N number:=&n; I number:=1; Begin While(i<=10) Loop P:=i*n; Dbms------(n!!*!!i!!=!!p); I:=i+1; End loop; End; Reverse of given number. Declare N number:=&n S number:=0 I number; Begin While(n>0) Loop I:=n mod 10; S:=(S*10)+I; N:=floor(n/10); End loop; Dbms------(s!!is reverse of a number); Declare N number:=&n; P number:=1; I number:=1; Begin For I in 1.n Loop P:=P*I; End loop; Dbms-----(factorial of!!n!!is!!p); End; Fibonaci series of a number Declare F1 number(3); F2 number(3); F3 number(3); Num number(3); Begin

F1:=0; F2:=1; F3:=0; Num:=1; While num<=10 Loop Dbms------(f3); F1:=f2; F2:=f3; F3:=f1+f2; Num:num+1; End loop; 01 palindrom(String) Declare Str varchar2(20); Str-2 number(2); Str-3 number(2); Str-4 varchar2(20):=; Begin Str:=madam; Str2:=length(Str); Str3:=Str2; For I in 1 ..Str2 Loop Str4:= Str4|| substr(str,Str3,1); Str3:=Str3-1; End loop; If Str=Str4 then Dbms______(palindrom); Else Dbms ______(not palindrom); End if; End; Output: Palindrome Statement processed

CURSORS --------------- Cursors are used when the sql select statement is expected to return more than one row Cursor is a buffer which will store the results of the return query. A cursor is a pointer to the context area memory on the server to process sql statement against database tables. There are two types of cursors Explicit cursor Implicit cursor Explicit Cursor In an explicit cursor a cursor name is explicitly assigned to a select statement through Cursor is a statement . To declare an explicit cursor ,you have to do in the DECLARATION section . Syntax Cursor curson name is select statement Select statement is not containing For example, we can define a cursor called c1 as below. CURSOR c1 IS SELECT course_number from courses_tbl where course_name = name_in; The result set of this cursor is all course_numbers whose course_name matches the variable called name_in.

FUNCTION
A function is a sub program, that carried out to compute the values. A function must have a return statement. In similarly procedure, we can pass the value by using IN , OUT, INOUT parameter in the function.

Syntax Create[OR replace] function(<arg1>[mod] <datatype>, . . . .) Return datatype is . . . . .Local declaration. . . . . . Begin --Body Exception End<function name> Eg: Create or replace function f1 Return char as Begin Return(test); End; Sql> select f1() from. . . .; 1 Find a given number factorial. Create or replace function factorial(n number) return number is begin if n=0 then return 1; else return n*factorial(n-1); end if; end factorial;

Command Line Sql> Connect

Sql>usrname/password Sql>variable pp number Sql> execute :p: =factorial(5); Sql> print pp;

Ans=120

2 Fibonaci series using in parameter Create or replace function fib(n in number)

Begin If n=0 or n=1 then return n; Else Return fib(n-1)+ fib(n-2); End if; End fib;

Command Line Sql>connect Sql>username/password Sql>begin for v in 0..10 100p Dbms_output.put_line(fib(v)); End loop End;

Out put 0 1 1 2 3 5 8 13 21 34 55

e.g create or replace package body p123 as function netsalary(id in emp.emp%type) return number is netsal emp.sal%type; begin select sum(sal) of sum(comm) into net-sal from emp where empno=id ; return (netsal); end net salary; procedure total(cid in emp.ename%type) is

ctr number(2):=0; dno number(4); dname varchar(30); curse my_cur is select empno,ename from emp where ename=cid; begin open my_cur; loop fetch my_cur into dno,dname; exit when my_cur%notfound; ctr:=ctr+1; dbms------(dno||dname || ddate); end loop; close mu_cur; end total; procedure tax(id emp.empno%type,tax out number) is net_sal number number(10); begin netsal:=net salary(id) if netsal<2000 then tax:=netsal*0.02; else if netsal<4000 thn tax:= netsal*0.04; end if; end tax;

end p123; \ Example Create or replace package p1 as Function f1 return char; Procedure prog1; End p1; \ //Package created Create or replace package body p1 as Function f1 return char As Begin Return(test); End f1; procedure prog1 as dbms_output.put_line(hello world); end prog1; end p1; command line sql> select p1.f1 from dual; sql> exec p1.prog1;

Вам также может понравиться