Question

The following question is based on this table named Customer Column Name     Type              CustId          Integer P

The following question is based on this table named Customer

Column Name     Type             

CustId          Integer Primary Key,

CustName        Varchar2(40),

Balance         Number

Gender          Varchar2(1)

Write the Stored Function named GetCustDetails.      

The function:

  • Has one parameter named pCustName.
  • Must return a text value
  • Must search for a customer row that has a CustName value equal to pCustName
    • If a match is found return the customer's name and balance in the following format:

Customer Name: Fred   Gender M   Balance 20000

  • If a match is not found return the string "Customer not Found"
  • If more that one match is found return the string "Count greater than 1"
  • Must use Exceptions and Late Error Raising

You may only use a maximum of one SELECT statement in the function.

Homework Answers

Answer #1

Creating a PL/SQL function

PL/SQL function is a reusable program unit stored as a schema object in the Oracle Database. The following illustrates the syntax for creating a function:

CREATE [OR REPLACE] FUNCTION function_name (parameter_list)

    RETURN return_type

IS

Calling a Function

While creating a function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task. When a program calls a function, the program control is transferred to the called function.

A called function performs the defined task and when its return statement is executed or when the last end statement is reached, it returns the program control back to the main program.

Eg.)

To call a function, you simply need to pass the required parameters along with the function name and if the function returns a value, then you can store the returned value. Following program calls the function totalCustomers from an anonymous block −

DECLARE 
   c number(2); 
BEGIN 
   c := totalCustomers(); 
   dbms_output.put_line('Total no. of Customers: ' || c); 
END; 
/

PL/SQL Exceptions

PL/SQL treats all errors that occur in an anonymous block, procedure, or function as exceptions. The exceptions can have different causes such as coding mistakes, bugs, even hardware failures.

Here is the basic syntax of the exception-handling section:

BEGIN

    -- executable section

    ...

    -- exception-handling section

    EXCEPTION

        WHEN e1 THEN

            -- exception_handler1

        WHEN e2 THEN

            -- exception_handler1

        WHEN OTHERS THEN

            -- other_exception_handler

END;

In this syntax, e1, e2 are exceptions.

Customer Table

Column Name     Type             

CustId Integer Primary Key,

CustName Varchar2(40),

Balance Number

Gender Varchar2(1)

Write the Stored Function named GetCustDetails.      

The function:

  • Has one parameter named pCustName.
  • Must return a text value

Answer:

CREATE OR REPLACE FUNCTION GetCustDetails(pCustName IN VARCHAR2(40))

RETURN VARCHAR2(40)

IS

BEGIN

RETURN(‘Welcome’| pCustName);

END

/

O/P Function created.

Calling function:

DECLARE

msg VARCHAR2(40);

BEGIN

msg:= GetCustDetails(‘Raj’)

dbms_output.put_line(msg);

END

O/P Welcome Raj

  • Must search for a customer row that has a CustName value equal to pCustName
    • If a match is found return the customer's name and balance in the following format:

Customer Name: Fred   Gender M   Balance 20000

Answer:

SELECT

customername,

balance

FROM

Customer

WHERE

CustName =pCustName

  • If a match is not found return the string "Customer not Found"

DECLARE

   CustId Customers.id%type := 8;

   CustName CustomerS.Name%type;

   Balance Customers.Balance%type :=8
   Gender Customers.Gender%type;

BEGIN

   SELECT name, id ,gender,balance INTO CustName,CustId,Gender,Balance

   FROM customers

   WHERE CustName = pCustname;
    DBMS_OUTPUT.PUT_LINE ('Customer Name: ' || CustName||'\t'||'Gender'||Gender||'\t'||'Balance'||Balance);

EXCEPTION

   WHEN no_data_found THEN

      dbms_output.put_line('Customer not found!');

   WHEN others THEN

      dbms_output.put_line('Error!');

END;

/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these classes: Product, Customer, and Store. All data members of each class should be marked as **private** (a leading underscore in the name). Since they're private, if you need to access them from outside the class, you should do so via get or set methods. Any get or set methods should be named per the usual convention ("get_" or "set_" followed by the name of...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code which will read in strings from nemo.txt, then call Lab08(string) on each line of nemo.txt. Your job is to write Lab08.m. The Lab08 function will take in a string as input and will return an integer n as output. The value of n will be the nth word in the input string, where n is the location of the substring 'Nemo'. Please note that...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...