PL-SQL BASIC PROGRAMS

download PL-SQL BASIC PROGRAMS

of 2

Transcript of PL-SQL BASIC PROGRAMS

  • 7/29/2019 PL-SQL BASIC PROGRAMS

    1/2

    1) Display a Message using a simple PL/SQL

    begin

    Dbms_output.put_line('Have a nice day');

    end;

    2) Display a Message with a String Variable using PL/SQL

    declare

    message varchar2(30) := 'Welcome to PL/SQL Programming';

    begin

    dbms_output.put_line(message);end;

    3) Write PL/SQL block that perform Sum of two numbers and displays it using

    concatenation Operator.

    declare

    a integer:=25;

    b integer:=32;

    c integer;

    begin

    c := a+b;

    dbms_output.put_line('a + b = ' || c);end;

    4) Write a PL/SQL block that perform sum of two numbers with reading values from

    keyboard.

    declare

    a integer:=:a;

    b integer:=:b;

    c integer;

    begin

    c := a+b;

    dbms_output.put_line('a = ' || a);

    dbms_output.put_line('b = ' || b);

    dbms_output.put_line(a || ' + ' || b || ' = ' || c);

    end;

    5) Write a PL/SQL block to find a given number is even or odd.

    declare

    num integer:=:num;

    begin

    if (mod(num,2)=0) then

    dbms_output.put_line( num || ' is even number.');

    else

    dbms_output.put_line( num || ' is odd number.');

    end if;

    end;

    6) Write a PL/SQL block to read a number and print the number is

    zero/positive/negative.

    declare

    num integer := :num;

    begin

    if (num > 0) then

    dbms_output.put_line( num || ' is positive.');

    elsif (num < 0) then

    dbms_output.put_line( num || ' is negetive.');else

    dbms_output.put_line( num || ' is equal to zero.');

    end if;

    end;

    7) Write a program to print the grade with reading the marks.

    declare

    marks integer := :marks;

    begin

    if (marks >= 70) then

    dbms_output.put_line('You got Distinction');

    elsif (marks >= 60) thendbms_output.put_line('You got First Class');

    elsif (marks >= 50) then

    dbms_output.put_line('You got Second Class');

    elsif (marks >= 35) then

    dbms_output.put_line('You got Third Class');

    else

    dbms_output.put_line('You failed in the Subject');

    end if;

    end;

    8) Write a program to print the biggest of three numbers.

    declare

    a integer := :a;

    b integer := :b;

    c integer := :c;

    begin

    if ( a > b ) then

    if ( a > c) then

    dbms_output.put_line( a || ' is the biggest number ');

    else

    dbms_output.put_line( c || ' is the biggest number ');

    end if;

    else

    if ( b > c) thendbms_output.put_line( b || ' is the biggest number ');

  • 7/29/2019 PL-SQL BASIC PROGRAMS

    2/2

    else

    dbms_output.put_line( c || ' is the biggest number ');

    end if;

    end if;

    end;

    9) Write a program to print the reverse of a given number.

    DECLARE

    num1 integer;

    num2 integer;digit integer;

    rev integer :=0;

    BEGIN

    dbms_output.put_line('Enter the Number: ' );

    num1 := :num;

    num2 := num1;

    WHILE (num1 > 0)

    LOOP

    digit := num1 mod 10;

    rev := (rev*10)+digit;

    num1 := trunc(num1/10);END LOOP;

    dbms_output.put_line('The reverse of ' || num2 || ' is ' || rev);

    END;

    10) Write a PL/SQL Block that calculates Factorial of a given number.

    declare

    num integer:=10;

    fact integer:=1;

    begin

    for i in 1..num

    loop

    fact:=fact*i;

    end loop;

    dbms_output.put_line('factorial of ' || num || ' is ' || fact);

    end;

    11) Write a PL/SQL Block that calculates Factorial of a given number with

    Exceptional Handling.

    declare

    num integer := :num;

    fact integer := 1;

    begin

    for i in 1 .. num

    loopfact := fact*i;

    end loop;

    dbms_output.put_line('factorial of ' || num || ' is ' || fact);

    exception

    when value_error then

    dbms_output.put_line('Over flow of number');

    end;

    12) . Display Details of an Employee given the Employee Number (using Implicit

    Cursor).

    DECLARE

    v_eno number (3);

    v_ename VARCHAR2(20);

    v_salary number(10);

    v_no number:=:Enumber;

    BEGIN

    SELECT empno,ename,salary INTO v_eno, v_ename, v_salary

    FROM employee

    WHERE empno=v_no;

    DBMS_OUTPUT.PUT_LINE(v_eno||' '||v_ename||' '||v_salary);

    ExceptionWhen no_data_found then

    DBMS_OUTPUT.PUT_LINE(' no employee found ');

    END;