Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is actually caused by your check:</p> <pre><code> SELECT COUNT (cus_code) INTO value_check FROM customer WHERE cus_code = temp_ccode; </code></pre> <p>The above query will <em>never</em> return NULL, which is what you are checking for. If no values in the table match the parameter <code>temp_ccode</code> then the <code>value_check</code> will be 0. This in turn means your IF statement is incorrect and this causes your error later in your code.</p> <p>There's a simpler and more efficient way of doing this though. You can use <a href="http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/static.htm#LNPLS528" rel="nofollow">SQL%ROWCOUNT</a> to find out how many rows were effected by your UPDATE. If the return value is 0 then your customer doesn't exist. </p> <pre><code>create or replace procedure cust_pay ( Ptemp_ccode in number , Ppay_amount in number ) is l_balance number; begin update customer set cus_balance = cus_balance - Ppay_amount where cus_code = Ptemp_ccode; if SQL%ROWCOUNT = 0 then dbms_output.put_line('The customer was not found.'); else select cus_balance into l_balance from customer where cus_code = temp_ccode; if l_balance &lt; 0 then dbms_output.put_line('the client owes us ' || l_balance); else dbms_output.put_line('customer new balance is ' || l_balance); end if; end if; end; </code></pre> <p>There's no need to handle the NO_DATA_FOUND exception in the <code>select... into ...</code> here as you've already guaranteed that the <code>cus_code</code> exists by your UPDATE statement.</p> <p>Please note the other changes I've made:</p> <ol> <li>Different naming conventions for parameters and variables so it's clear in the code which is which.</li> <li>Removal of the additional nested PL/SQL block, which was unnecessary.</li> </ol> <p>Generally speaking you should never use <code>dbms_ouput.put_line</code> in a PL/SQL block as you <em>have</em> to be there to see what's happening. It's fine for debugging processes but is fairly useless in production code.</p> <p>It's also possible to use the <a href="http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/return_statement.htm#LNPLS01340" rel="nofollow">RETURN statement</a> to avoid the nested IF statements, which I think makes the code cleaner and easier to read; though this is a judgement call.</p> <pre><code>create or replace procedure cust_pay ( Ptemp_ccode in number , Ppay_amount in number ) is l_balance number; begin update customer set cus_balance = cus_balance - Ppay_amount where cus_code = Ptemp_ccode; if SQL%ROWCOUNT = 0 then return; end if; select cus_balance into l_balance from customer where cus_code = temp_ccode; if l_balance &lt; 0 then dbms_output.put_line('The client owes us ' || l_balance); else dbms_output.put_line('New balance is ' || l_balance); end if; end; </code></pre> <p>All of this assumes that your CUSTOMER table is unique on <code>cus_code</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload