#Procedure To Find Square of A Given No

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

#procedure to find square of a given no

declare

a number;

procedure square(x in out number)is

begin

x:=x*x;

end;

begin

a:=23;

square(a);

dbms_output.put_line('square of (23) is:'||a);

end;

#PL/SQL FUNCTIONS to count total no of customers

CREATE OR REPLACE FUNCTION totalcustomers

RETURN number IS

total number(2):= 0;

BEGIN

SELECT count(*)

INTO total

FROM customers;

RETURN total;

END;

/
DECLARE

c number(2);

BEGIN

c :=totalcustomers();

dbms_output.put_line('total no of customers:' || c);

END;

#trigger to update salary

create or replace trigger abc

BEFORE DELETE OR INSERT OR UPDATE ON customers

FOR EACH ROW

when(NEW.id>0)

DECLARE

sal_diff number;

BEGIN

sal_diff:= :NEW.salary - :OLD.salary;

dbms_output.put_line('new salary:'|| :NEW.salary);

dbms_output.put_line('old salary:'|| :OLD.salary);

dbms_output.put_line('salary difference:' || sal_diff);

end;

insert into customers values(8,'kirti',28,'bihar',7500.00);

update customers

set salary=salary+500

where id=2;
delete from customers

where id=2;

## Write a PL/SQL block to display the salary of a particular employee.


insert into employee77 values('1234566','shivani','03-march-1997','F','Patna',20000,2);

insert into employee77 values('888665555','vani','03-Jan-1996','F','Karnataka',25000,3);

insert into employee77 values('123454566','shivya','01-oct-1995','F','Patna1',30000,4);

declare

sal number(12,2);

ssn1 char(9):='888665555';

begin

select salary into sal

from employee77

where ssn=ssn1;

dbms_output.put_line('the employee'||ssn1||''||'has'||sal||'salary');

end;

## Write a trigger to compare an employee’s age during insert or update


operations:

create table emp777(

ssn char(9) not null,

ename varchar(20) not null,

bdate date,

sex char,

address varchar(22) not null,


salary decimal(10,2) not null,

dno int not null,

primary key(ssn));

CREATE OR REPLACE TRIGGER salary2

BEFORE DELETE OR INSERT OR UPDATE ON emp777

FOR EACH ROW

WHEN(new.salary>2500)

DECLARE

Inform_supervisor number;

BEGIN

dbms_output.put_line('salary_voilation');

END;

insert into emp777 values('123456789','kirti','19-jan-1998','f','patna',2600,7);

update emp777

set salary=80000

where ssn='123456789';

## 2.Write a PL/SQL block to display the salary of a particular employee. The social
security number of the employee must be taken as input from the user

DECLARE

sal1 decimal(10,2);

ssn1 char(9);
begin

ssn1:= :x;

select salary into sal1

from emp8

where ssn=ssn1;

dbms_output.put_line(sal1);

dbms_output.put_line(ssn1);

dbms_output.put_line('the employee'||ssn1||''||'has salary'||sal1);

end;

## Write a trigger to compare an employee’s age during insert or update


operations:

CREATE OR REPLACE TRIGGER SAL777

BEFORE INSERT OR UPDATE ON emp8

FOR EACH ROW

WHEN(new.salary>25000)

DECLARE

inform number;

BEGIN

dbms_output.put_line('salary violation');

end;
insert into emp8 values('23456178','abcde','01-jan-
1999','f','bangalore',250000,9);

## Write a PL/SQL block to get 3 marks and compute the grade and display the
interpretation of the grade (Average is >=40 and <60 – Pass, >=60 and < 75 –
Good, >=75 and <90 – Very Good, >=90 – Excellent. A-Excellent, B-Very Good, C-
Good, D-Pass, F-Fail).

declare

grade char(1);

begin

grade :=:g;

CASE grade

when 'A' then dbms_output.put_line('Excellent');

when 'B' then dbms_output.put_line('Very good');

when 'C' then dbms_output.put_line('GOOD');

when 'D' then dbms_output.put_line('PASS');

when 'F' then dbms_output.put_line('fail');

else dbms_output.put_line('no such grade');

end case;

end;

## ) Write a PL/SQL program that includes a procedure to find the maximum of


two values.

declare
a number;

b number;

c number;

procedure max(x in number,y in number,z out number)is

begin

if x>y then

z:=x;

else

z:=y;

end if;

end;

begin

a:=23;

b:=26;

max(a,b,c);

dbms_output.put_line('max of (23,26)is:'||c);

end;

##Write a PL/SQL program that includes a function to find the minimum


of two values Function to find maximum of 2 numbers
DECLARE

a number;
b number;

c number;

FUNCTION findMax(x IN number, y IN number)

RETURN number

IS

z number;

BEGIN

IF x > y THEN

z:= x;

ELSE

Z:= y;

END IF;

RETURN z;

END;

BEGIN

a:= 23;

b:= 45;

c := findMax(a, b);

dbms_output.put_line(' Maximum of (23,45): ' || c);

END;

/
## 7b.create a trigger to update product price history table when the price of the
product is updated in product table.

create table product2(

product_id number(5), product_name varchar2(32),

supplier_name varchar2(32), unit_price number(7,2) );

create table product_price_history2

(product_id number(5), product_name varchar2(32), supplier_name varchar2(32),

unit_price number(7,2) );

insert into product2 values(1,'maggie','nestle',20)

insert into product2 values(2,'baggie','estle',200)

insert into product2 values(3,'caggie','mestle',60)

insert into product2 values(4,'daggie','nnestle',80)

insert into product_price_history2 values(6,'biscuit','abcd',80)

insert into product_price_history2 values(9,'bicuit','aabcd',70)

insert into product_price_history2 values(10,'bscuit','abbcd',90)

insert into product_price_history2 values(5,'biscit','abccd',60)

insert into product_price_history2 values(1,'iscuit','abcdd',50)

CREATE or REPLACE TRIGGER price2


BEFORE UPDATE OF unit_price

ON product2

FOR EACH ROW

BEGIN

INSERT INTO product_price_history2

VALUES

(:old.product_id,

:old.product_name,

:old.supplier_name,

:old.unit_price);

END;

UPDATE product2

SET unit_price = 800

WHERE product_id = 1

select * from product2

You might also like