100% found this document useful (1 vote)
284 views4 pages

PL SQL Cognizant Dumps

The document contains examples of PL/SQL packages and functions to add, delete and retrieve customer data from a database table including procedures to get order details, check for palindromes, and count words and characters in a string.

Uploaded by

iamfaisal360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
284 views4 pages

PL SQL Cognizant Dumps

The document contains examples of PL/SQL packages and functions to add, delete and retrieve customer data from a database table including procedures to get order details, check for palindromes, and count words and characters in a string.

Uploaded by

iamfaisal360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#1-ADD CUSTOMER- PACKAGE:

create PACKAGE pkg_customer


as
PROCEDURE addCustomer (c_id t_customer.cust_id%type, c_name t_customer.cust_
name%type, c_addr t_customer.cust_address%type);
end pkg_customer;
/

create PACKAGE body pkg_customer


as
PROCEDURE addCustomer(c_id t_customer.cust_id%type, c_name t_customer.cust_n
ame%type, c_addr t_customer.cust address%type)
as
begin
insert into t_customer(cust_id, cust_name, cust_address)
values (c_id, c_name,c_addr);
end addCustomer;
end pkg_customer;
/

#2-CHARCOUNT FUNCTION:

CREATE OR REPLACE FUNCTION CHARCOUNT(str VARCHAR2)


RETURN NUMBER
AS
r NUMBER:=0;
x NUMBER;
BEGIN
X := REGEXP_COUNT(str, ' ');
r := LENGTH (str)-x;
RETURN r;
END;
/

----------------------------------OR----------------------------------

create or replace FUNCTION characterCount(str IN VARCHAR2)


return number
as
character_val number :=0;
begin
for i in 1..length(str) loop
character_val:=character_val+1;
end loop;
return character_val;
end;
/

#3-DELETE CUSTOMER- PACKAGE:

create PACKAGE pkg_delcustomer


as
PROCEDURE deleteCustomer (c_id t_customer.cust_id%type);
end pkg_delcustomer;
/

create PACKAGE body pkg_delcustomer


as
PROCEDURE deleteCustomer (c_id t_customer.cust_id%type)
as
begin
delete from t_customer where cust_id=c_id;
exception
when no_data_found then
dbms_output.put_line( 'NO CUSTOMER AVAILABLE');
end deleteCustomer;
end pkg_delcustomer;
/

#4-GET ORDER DETAILS- PROCEDURE:

set serveroutput on;


create or replace PROCEDURE getorderDetails(p_order_id IN t_order_details.orde
r_id%TYPE)
as
cursor cur_tod is select item_id, quantity from t_order_details where order_
id=p_order_id;
a_item_id t_order_details.item_id%type;
a_quantity t_order_details.quantity%type;
begin
dbms_output.put_line('Order ID : ' || p_order_id);
dbms_output.put_line('Item ID' || ' ' || 'Quantity');
dbms_output.put_line ('---------------------------------------------------
');
open cur_tod;
loop
fetch cur_tod into a_item_id, a_quantity;
exit when cur_tod%notfound;
dbms_output.put_line (a_item_id || ' ' || a_quantity);
end loop;
close cur_tod;
exception
when no_data_found then
dbms_output.put_line('NA');
end;
/

#5-PALINDROME:

create or replace function palindrome(str IN varchar2)


return boolean
as
c varchar2(50);
rv varchar2(50);
begin
for i in reverse 1..length(str) loop
c:=substr(str,i,1);
rv:= rv || c;
end loop;
if rv=str then
return true;
else
return false;
end if;
end;
/

#6-WORDCOUNT FUNCTION:

CREATE OR REPLACE FUNCTION wordcount(str VARCHAR2)


RETURN NUMBER
AS
r number;
BEGIN
str := LTRIM(str, ' ');
R:= REGEXP_COUNT(STR, ' ')+1;
RETURN R;
END;
/

------------------------------OR-----------------------------------------

CREATE OR REPLACE FUNCTION wordcount(str VARCHAR2)


RETURN NUMBER
AS
r number:=1;
c CHAR;
BEGIN
FOR i IN 1..LENGTH(STR) LOOP
C:= SUBSTR(str, i, 1);
IF C=' ' THEN
R:= R+1;
END IF;
END LOOP;
RETURN R;
END;
/

----------------------------------OR----------------------------------

create or replace FUNCTION wordCount(str IN VARCHAR2)


return number
as
val_words number:=1;
z char;
begin
for i in 1..length(str) loop
z:=substr(ltrim(str,' '),i,1);
if z=' ' then
val_words:=val_words+1;
end if;
end loop;
return val_words;
end;
/

You might also like