If-Else in PL/SQL - oracle

I want to write a procedure to perform withdraw operation that only permits a withdrawal, if there is sufficient funds in the account then update the Account table and print the message, 'Transaction successful.' else print, 'Insufficient Amount.' . The procedure should take Account_id and withdrawal amount as input.
Account:
ACCNO NUMBER PK
CUSTOMER_NAME VARCHAR2(30)
BALANCE NUMBER(15,2)
12345 Williams 23455.6
23456 Robert 43221
34521 John 23449
Functional Requirement:
procedure withdraw(ano number , amt number)
Sample input:
withdraw(12345, 2000);
Sample output:
Transaction successful.
I tried to write this code which is as follows-
set serveroutput on;
create or replace procedure withdraw(ano number, amt number) is withdraw_operation account%rowtype;
begin
select * into withdraw_operation from account
if (amt > balance)
then dbms_output.put_line('Transaction successful');
else dbms_output.put_line('Insufficient Amount');
end if;
end;
But this is not showing any output nor error, please help. Thanks in advance!

this is not showing any output nor error
This is hard to believe because what you posted isn't correct.
Anyway: from what you posted, should be something like this:
Sample data:
SQL> SET SERVEROUTPUT ON;
SQL>
SQL> SELECT * FROM account;
ACCNO CUSTOMER BALANCE
---------- -------- ----------
12345 Williams 23455,6
23456 Robert 43221
34521 John 23449
Procedure:
SQL> CREATE OR REPLACE PROCEDURE withdraw (ano NUMBER, amt NUMBER)
2 IS
3 withdraw_operation account%ROWTYPE;
4 BEGIN
5 SELECT *
6 INTO withdraw_operation
7 FROM account
8 WHERE accno = ano;
9
10 IF amt > withdraw_operation.balance
11 THEN
12 DBMS_OUTPUT.put_line ('Transaction successful');
13 ELSE
14 DBMS_OUTPUT.put_line ('Insufficient Amount');
15 END IF;
16 END;
17 /
Procedure created.
Testing:
SQL> EXEC withdraw(12345, 2000);
Insufficient Amount
PL/SQL procedure successfully completed.
SQL>

Related

Stored procedure for money transfer in PLSQL

Hope this task find you well.
Please help me out with this,
My requirement is to transfer money from one account to another account which is in a same table.
the procedure should have 3 in parameters,
They are,
--> from_account_number
--> To_account_number
--> Credit_amount
Actual data:
acc_num acc_bal
12345 50000
67890 40000
Expected data:
eg: exec trans_sp(12345,67890,10000);
ac_num ac_bal
12345 40000
67890 60000
`trans_sp`
A simple option (with no controls at all; add them yourself):
Before:
SQL> select * from money;
ACCOUNT_NU AMOUNT
---------- ----------
12345 10000
65789 20000
86420 30000
Procedure:
SQL> create or replace procedure trans_sp (par_acc_from in varchar2, par_acc_to in varchar2, par_amount in number)
2 is
3 begin
4 update money set
5 amount = amount - par_amount
6 where account_number = par_acc_from;
7
8 update money set
9 amount = amount + par_amount
10 where account_number = par_acc_to;
11 end;
12 /
Procedure created.
Testing:
SQL> exec trans_sp('86420', '12345', 5000);
PL/SQL procedure successfully completed.
After:
SQL> select * from money;
ACCOUNT_NU AMOUNT
---------- ----------
12345 15000
65789 20000
86420 25000
SQL>

How to use procedure within package in PL/SQL

How to create a Package with Procedure in it? Procedure will take designation and incentive as input and update the employee salary by adding the incentive for the given designation. Display the number of employee records that have got updated, e.g. ‘3 employee record(s) are updated’.
I have a table EMPLOYEE, with EMP_ID, EMP_NAME, SALARY and DESIGNATION.
The given Functional Requirements are:
Package name as EMP_DESIGNATION, and
Procedure signature:
EMP_DETAILS(design employee.designation%TYPE, incentive number);
The code which I tried is -
set serveroutput on;
CREATE OR REPLACE PACKAGE EMP_DESIGNATION AS
PROCEDURE EMP_DETAILS(
design employee.designation%TYPE,
incentive NUMBER) IS
BEGIN
UPDATE employee SET employee.salary = employee.salary + incentive
WHERE employee.designation = design;
dbms_output.put_line(SQL%ROWCOUNT || ' employee record(s) are updated');
END EMP_DESIGNATION;
END;
/
The output is-
Warning: Package Body created with compilation errors.
BEGIN
*
ERROR at line 1:
ORA-04063: package body "P10017.EMP_DESIGNATION" has errors
ORA-06508: PL/SQL: could not find program unit being called:
"P10017.EMP_DESIGNATION"
ORA-06512: at line 2
How to solve this error? Please help. Thank you in advance.
PL/SQL: Statement ignored
You have to create a package specification first, then its body. Something like this:
SQL> CREATE OR REPLACE PACKAGE emp_designation
2 AS
3 PROCEDURE emp_details (design employee.designation%TYPE, incentive NUMBER);
4 END emp_designation;
5 /
Package created.
SQL>
SQL> CREATE OR REPLACE PACKAGE BODY emp_designation
2 AS
3 PROCEDURE emp_details (design employee.designation%TYPE, incentive NUMBER)
4 IS
5 BEGIN
6 UPDATE employee
7 SET employee.salary = employee.salary + incentive
8 WHERE employee.designation = design;
9
10 DBMS_OUTPUT.put_line (
11 SQL%ROWCOUNT || ' employee record(s) are updated');
12 END emp_details;
13 END emp_designation;
14 /
Package body created.
SQL>

Keep getting compilation error while creating the following procedure

Create a procedure named 'select_city' which accepts one input parameter user_id of type number and one output parameter city_details of type varchar. This procedure is used to display the city_details of user.If the user is from bangalore then display the city_details as 'User is from Bangalore',or if the user is from chennai then display the city_details as 'User is from Chennai', else display the city_details as 'User is from other cities'.
CREATE PROCEDURE select_city ( user_id IN user_details.id%type,
city_details OUT VARCHAR2(255) )
AS
BEGIN
SELECT CASE
WHEN city = 'Bangalore' THEN 'User is from Bangalore'
WHEN city = 'Chennai' THEN 'User is from Chennai'
ELSE 'User is from other cities'
END tmp_status INTO city_details
FROM contact cnt
WHERE cnt.id = user_id;
END;
OUT parameter shouldn't have size. Remove it.
Also (probably not related to error you got), consider using CREATE OR REPLACE because any subsequent CREATE will fail as the procedure - although invalid - already exists so you'd have to drop it first.
Sample table:
SQL> create table contact as
2 select 'Bangalore' city, 1 id from dual;
Table created.
Procedure:
SQL> create or replace procedure select_city
2 (user_id in number,
3 city_details out varchar2 --> no size here
4 )
5 as
6 begin
7 select case
8 when city = 'Bangalore' then
9 'User is from Bangalore'
10 when city = 'Chennai' then
11 'User is from Chennai'
12 else
13 'User is from other cities'
14 end tmp_status
15 into city_details
16 from contact cnt
17 where cnt.id = user_id;
18 end;
19 /
Procedure created.
Testing:
SQL> set serveroutput on;
SQL> declare
2 l_citydet varchar2(255);
3 begin
4 select_city(1, l_citydet);
5 dbms_output.put_line(l_citydet);
6 end;
7 /
User is from Bangalore
PL/SQL procedure successfully completed.
SQL>

plsql procedures while executing an error invalid identifier 00904

Error message
CREATE TABLE DEPARTMENT(DID INT,DNAME VARCHAR2(20),DLOC VARCHAR2(20));
INSERT INTO DEPARTMENT VALUES(101,'SRINATH','HINDUPUR');
INSERT INTO DEPARTMENT VALUES(102,'SAINATH','ANANTAPUR');
create or replace PROCEDURE ADD_DEPARTMENT
(P_DID IN DEPARTMENT.DID%TYPE,
P_DNAME IN DEPARTMENT.DNAME%TYPE,
P_DLOC IN DEPARTMENT.DLOC%TYPE,
P_ERROR_MSG OUT VARCHAR2)
IS
BEGIN
INSERT INTO DEPARTMENT(DID,DNAME,DLOC)VALUES(P_DID,P_DNAME,P_DLOC);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
P_ERROR_MSG:=SQLERRM;
END ADD_DEPARTMENT;
iam writing a simple procedure try to excute but it shows object_id:invalid identifier how i can solve
complete procedure execution image
One of your column names may be wrong in the parameter list/insert. Try just using varchar2 for all of them and see if that helps
create or replace PROCEDURE ADD_DEPT2
(P_DEPTNO IN VARCHAR2,
P_DNAME IN VARCHAR2,
P_LOC IN VARCHAR2,
P_ERROR_MSG OUT VARCHAR2)
IS
BEGIN
INSERT INTO DEPT1(DEPTNO,DNAME,LOC)VALUES(P_DEPTNO,P_DNAME,P_LOC);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
P_ERROR_MSG :=SQLERRM;
END ADD_DEPT2;
"it will come same error can be displayed"
No, no it won't. Let's prove it by running the code you posted.
Here is your table:
SQL> CREATE TABLE DEPARTMENT(DID INT,DNAME VARCHAR2(20),DLOC VARCHAR2(20));
Table created.
SQL> INSERT INTO DEPARTMENT VALUES(101,'SRINATH','HINDUPUR');
1 row created.
SQL> INSERT INTO DEPARTMENT VALUES(102,'SAINATH','ANANTAPUR');
1 row created.
SQL>
Here is your procedure:
SQL> create or replace PROCEDURE ADD_DEPARTMENT
2 (P_DID IN DEPARTMENT.DID%TYPE,
3 P_DNAME IN DEPARTMENT.DNAME%TYPE,
4 P_DLOC IN DEPARTMENT.DLOC%TYPE,
5 P_ERROR_MSG OUT VARCHAR2)
6 IS
7 BEGIN
8 INSERT INTO DEPARTMENT(DID,DNAME,DLOC)VALUES(P_DID,P_DNAME,P_DLOC);
9 COMMIT;
10 EXCEPTION
11 WHEN OTHERS THEN
12 P_ERROR_MSG:=SQLERRM;
13 END ADD_DEPARTMENT;
14 /
Procedure created.
SQL>
And lo! not only does it compile but it runs without error too.
SQL> set serveroutput on
SQL> declare
2 v varchar2(128);
3 begin
4 add_department(666, 'Area 51', 'Roswell', v);
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> select * from department;
DID DNAME DLOC
---------- -------------------- --------------------
101 SRINATH HINDUPUR
102 SAINATH ANANTAPUR
666 Area 51 Roswell
SQL>
So once again I ask, what is the problem?
"PLS-00905: object SCOTT.ADD_DEPARTMENT is invalid"
In SQL*Plus:
Connect as SCOTT
alter procedure add_department compile;
show errors
Or apply the equivalent sequence for whatever IDE you use to write your code.

create withdrawal procedure in oracle pl/sql

I want to create a withdrawal procedure in oracle pl/sql.
I have this table
ACCOUNT_NO BRANCH_NAME AMOUNT_BALANCE
---------- -------------------------------------------------- --------------
102 MA 32900
101 NA 32000
103 IA 50000
104 SA 45000
105 MSA 20000
I try to use this code
CREATE OR REPLACE PROCEDURE withdrawal_proc IS
con number(6);
con1 number(6);
bal1 number(20);
bal2 number(20);
begin
con := &con;
bal1 := &bal;
select Account_No, Amount_Balance into con1, bal2 from Accounts where Account_No=con;
if (bal2 < bal1)
dbms_output.put_line('the amount enterd is more than the amount balance');
else
update Accounts set Amount_Balance=(bal1-bal2) where con =con1;
end if;
dbms_output.put_line('Money has been Withdraw succesfully');
END;
/
but there is a Warning: Procedure created with compilation errors.
You can't use SQL*Plus-style variables such as &con and &bal1 in stored procedures. In this case the con and bal values should probably be passed to the procedure as parameters:
CREATE OR REPLACE PROCEDURE withdrawal_proc(account_no_in IN NUMBER(6),
bal_in IN NUMBER(20))
IS
current_balance number(20);
BEGIN
select Amount_Balance
into current_balance
from Accounts
where Account_No = account_no_in;
if current_balance < bal_in then
dbms_output.put_line('The amount entered is more than the amount balance');
else
update Accounts
set Amount_Balance = bal_in - current_balance
where Account_No = account_no_in;
end if;
dbms_output.put_line('Money has been withdrawn successfully');
END;
You also used improper syntax for your IF statement - compare the original code with the version above.
In addition, note that I suspect that the account balance computation may be incorrect. You may need to debug this a bit.
Best of luck.

Resources