create procedure p5(age1 IN number) as
BEGIN
if age1>18 then
insert into t values ( age1 );
else
dbms_output.putline('age should be high');
end if;
end p5;
Warning: Procedure created with compilation errors.
I have tried executing this and i am getting errors listed below
SQL> exec p5(20);
BEGIN p5(20); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00905: object SYSTEM.P5 is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I am still getting these errors
There are multiple problems,
create or replace procedure p3(age1 IN number) as --you dont need (3) here. Also closing braces are missing.
BEGIN
if age1>18 then
insert into t values ( age1 );
else
dbms_output.put_line('age should be high'); --you were using putline.
end if;
end p3; --your proc name is p2 but you are endin p3. Not needed. Just END will also do.
I tried running it locally and it is working fine.
create table t (age number(3));
Table T created.
create procedure p3(age1 IN number) as
BEGIN
if age1>18 then
insert into t values ( age1 );
else
dbms_output.put_line('age should be high');
end if;
end p3;
Procedure P3 compiled
set serveroutput on;
exec p3(23);
PL/SQL procedure successfully completed.
exec p3(17);
PL/SQL procedure successfully completed.
age should be high
select * from t;
AGE
23
The error is here:
dbms_output.put_line
instead of
dbms_output.putline
You missed closing parenthesis after p3(age1 IN number(3). Try following :
create procedure p3(age1 IN number) as
BEGIN
if age1>18 then
insert into t values ( age1 );
else
dbms_output.put_line('age should be high'); -- it should be put_line
end if;
end p3; --correct procedure name
Related
I'm trying to create a procedure in Oracle. I want to be able to call on the procedure with a variable input and have a statement read. I am in the process of learning Oracle SQL, so I'm not sure where I'm going wrong.
This is my code:
create or replace procedure gradeCheck(grade char(1))
Begin
Case
When grade = 'A' then DBMS_OUTPUT.PUT_LINE('Great Work')
else DBMS_OUTPUT.PUT_LINE('Not great work')
end case;
end gradeCheck;
/
Begin
gradeCheck(A);
end;
/
I want the input to be 'A' (the grade assignment) and to get the text output. I am getting a message that the GRADECHECK procedure is being compiled. But it won't let me call it with an input.
You want use IF not CASE
You are missing the IS keyword before BEGIN
You need to terminate statements with ;
The CHAR data type does not have a length in the signature.
Like this:
create procedure gradeCheck(
grade CHAR
)
IS
BEGIN
IF grade = 'A' THEN
DBMS_OUTPUT.PUT_LINE('Great Work');
ELSE
DBMS_OUTPUT.PUT_LINE('Not great work');
END IF;
END gradeCheck;
/
Then you need to use quotes around the string:
Begin
gradeCheck('A');
end;
/
db<>fiddle here
Your issues
You are missing a word is in the create or replace statement
If the input variable is string ( char ) then you have to enclose in quotes the value when you call the procedure.
Use varchar2 instead of char, although it is not mandatory.
; must be at the end of dbms_output
If you run it with sqlplus, use set serveroutput on to enable the output, that you can see the message.
Then
create or replace procedure gradeCheck(grade in varchar2)
is
Begin
case when grade = 'A'
then
DBMS_OUTPUT.PUT_LINE('Great Work');
else
DBMS_OUTPUT.PUT_LINE('Not great work');
end case;
end gradeCheck;
/
set serveroutput on
Begin
gradeCheck('A');
end;
/
Test Case
SQL> l
1 create or replace procedure gradeCheck(grade in varchar2)
2 is
3 Begin
4 case
5 when grade = 'A' then
6 DBMS_OUTPUT.PUT_LINE('Great Work');
7 else
8 DBMS_OUTPUT.PUT_LINE('Not great work');
9 end case;
10* end gradeCheck;
SQL> /
Procedure created.
SQL> begin
2 gradeCheck('A');
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> r
1 begin
2 gradeCheck('A');
3* end;
Great Work
PL/SQL procedure successfully completed.
So I have this function:
create or replace function get_authorization(
p_pnr in bankcustomer.pnr%type,
p_knr in account.cnr%type)
return number
as
v_authorization bankcustomer.pnr%type;
begin
select count(*)
into v_authorization
from bankcustomer,account
where pnr = p_pnr
and cnr = p_cnr;
return v_authorization;
exception
when no_data_found then
return -1;
end;
This returns 1 or 0 if allowed. I need to do a procedure which adds a row in a table called withdraw and that procedure needs to call get_authorization to check if the customer is allowed. This is what i have so far:
create or replace procedure do_withdraw(
p_pnr in withdraw.pnr%type,
p_belopp in withdraw.belopp%type)
as
declare
authorization exception;
begin
insert into uttag(pnr,amount)
values((select pnr from account),p_amount); /*pnr is foreign key in withdraw*/
if user not in (select get_amount(p_pnr) from dual) then
raise authorization;
end if;
exception
when authorization then
raise_application_error(-20007,'Unauthorized!');
commit;
end;
I get alot of error messages and specifically on declare. I really cant wrap my head around this :(
You have few problems with your code -
create or replace procedure do_withdraw(
p_pnr in withdraw.pnr%type,
p_belopp in withdraw.belopp%type)
as
-- declare -- Declare keyword is not used in procedure
authorization exception;
begin
insert into uttag(pnr,amount)
values((select pnr from account), -- This might return multiple rows, So you have to add a where clause in this query.
p_amount); /*pnr is foreign key in withdraw*/
if user <> get_authorization(p_pnr) then -- You are checking current user with amount which should be corrected.
raise authorization;
end if;
commit; -- Commit should be last sattement of procedure
exception
when authorization then
raise_application_error(-20007,'Unauthorized!');
Rollback; -- In exception you should use Rollabck instead of Commit;
end;
So ive tried what you said and think i know what you mean:
create or replace procedure do_withdraw(
p_pnr in withdraw.pnr%type,
p_amount in withdraw.amount%type)
as
-- declare -- Declare keyword is not used in procedure
authorization exception;
begin
insert into withdraw(pnr,amount)
values((select pnr from account where pnr = p_pnr), -- This might return multiple rows, So you have to add a where clause in this query.
p_amount); /*pnr is foreign key in withdraw*/
if user not in (select get_authorization(p_pnr)) then -- You are checking current user with amount which should be corrected.
raise authorization;
end if;
commit; -- Commit should be last sattement of procedure
exception
when authorization then
raise_application_error(-20007,'Unauthorized!');
Rollback; -- In exception you should use Rollabck instead of Commit;
end;
Now i get error: Line/Col: 11/51 PLS-00103: Encountered the symbol ")" when expecting one of the following:. ( , * % & - + /
I am trying to create table with procedure CREATE_TABLE and then insert the information into the created table with procedure PRINT_INFO but I am getting an exception:
Errors: PROCEDURE PRINT_INFO Line/Col: 4/3 PL/SQL: SQL Statement
ignored Line/Col: 4/15 PL/SQL: ORA-00942: table or view does not exist
Errors: PROCEDURE CREATE_TABLE Line/Col: 5/3 PLS-00103: Encountered
the symbol "CREATE" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma raise
return select update while with << continue close current
delete fetch lock insert open rollback savepoint set sql execute
commit forall merge pipe purge json_exists json_value json_query
json_object json_array
And here is my code example:
CREATE OR REPLACE PROCEDURE PRINT_INFO
IS
BEGIN
INSERT INTO TABLE_T (TABLE_ID, MESSAGE) VALUES (1, 'Hello World!');
END PRINT_INFO;
/
CREATE OR REPLACE PROCEDURE CREATE_TABLE
IS
BEGIN
CREATE TABLE TABLE_T(
TABLE_ID NUMBER NOT NULL,
MESSAGE VARCHAR2(25),
PRIMARY KEY(TABLE_ID)
);
PRINT_INFO;
END CREATE_TABLE;
/
EXEC CREATE_TABLE;
Where could be the problem? How can I get rid of an exception?
Both procedures have to use dynamic SQL:
print_info because it inserts into a table which - at compilation time - doesn't exist yet
create_table because it runs DDL and - in order to do that - you need to use dynamic SQL
Therefore:
SQL> CREATE OR REPLACE PROCEDURE PRINT_INFO
2 IS
3 BEGIN
4 execute immediate q'[INSERT INTO TABLE_T (TABLE_ID, MESSAGE) VALUES (1, 'Hello World!')]';
5 END PRINT_INFO;
6 /
Procedure created.
SQL> CREATE OR REPLACE PROCEDURE CREATE_TABLE
2 IS
3 BEGIN
4 execute immediate 'CREATE TABLE TABLE_T(' ||
5 'TABLE_ID NUMBER NOT NULL, ' ||
6 ' MESSAGE VARCHAR2(25), ' ||
7 ' PRIMARY KEY(TABLE_ID) ' ||
8 ')';
9
10 PRINT_INFO;
11 END CREATE_TABLE;
12 /
Procedure created.
SQL> EXEC CREATE_TABLE;
PL/SQL procedure successfully completed.
SQL> SELECT * FROM table_t;
TABLE_ID MESSAGE
---------- -------------------------
1 Hello World!
SQL>
You have to declare a string and assign your ddl to that variable then use
execute immediate your_variable ;
I have little problem. I am trying to insert value into table. This is working. But I would like to control if value id_trainer is existing in another table. I want this -> execute insertClub(1, 5, 'someName'); -> and if id_trainer 5 not exists in table Trainer, procedure gives me message about this. (I tried to translate it into eng. lng., so you can find some gramm. mistakes)
create or replace procedure insertClub
(id_club in number, id_trainer in number, clubName in varchar2)
is
begin
declare counter number;
select count(*) into counter from trianer tr where tr.id_trainer = id_trainer;
if counter = 0 then
DBMS_OUTPUT.PUT_LINE('Trainer with this ID not exists');
end if;
insert into club values(id_club, id_trainer, clubName);
exception
when dup_val_on_index then
DBMS_OUTPUT.PUT_LINE('Dup ID');
end;
/
There is some error in the procedure. Please run below code to create procedure:
create or replace procedure insertClub
(id_club in number, id_trainer in number, clubName in varchar2)
is
counter number;
begin
select count(*) into counter from trianer tr where tr.id_trainer = id_trainer;
if counter = 0 then
DBMS_OUTPUT.PUT_LINE('Trainer with this ID not exists');
end if;
insert into club values(id_club, id_trainer, clubName);
exception
when dup_val_on_index then
DBMS_OUTPUT.PUT_LINE('Dup ID');
end;
/
I know these errors are most often caused by typos; that's what I've been finding at least. If my problem is a typo, I cannot see it after ~30 minutes of looking, it's driving me crazy.
My question is: am I doing something fundamentally wrong, or can you see a typo?
PL/SQL:
CREATE OR REPLACE PROCEDURE Replenish_Stock(p_id VARCHAR2, n INT)
AS
no_such_id EXCEPTION;
CURSOR pc IS
SELECT Product_ID FROM Products;
BEGIN
IF p_id IN pc THEN
UPDATE Products
SET Stock_Level = Stock_Level + n
WHERE product_id = p_id;
ELSE
RAISE no_such_id;
END IF;
EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUT_LINE('Bad integer input, ignoring procedure call.');
WHEN no_such_id THEN
DBMS_OUTPUT.PUT_LINE('Bad Product id, ignoring procedure call.');
END;
/
Error code:
Error(7,14): PLS-00103: Encountered the symbol "PC" when expecting one of the following: (
Thanks for any help.
Your usage of IN and CURSOR is not right. the below should work for you. You can just use SQL%ROWCOUNT to see if the update query impact any rows in the table.
CREATE OR REPLACE PROCEDURE Replenish_Stock(p_id VARCHAR2, n INT)
AS
no_such_id EXCEPTION;
Rows_Updated NUMBER;
BEGIN
UPDATE Products
SET Stock_Level = Stock_Level + n
WHERE product_id = p_id;
IF( SQL%ROWCOUNT = 0) THEN
RAISE no_such_id;
END IF;
EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUT_LINE('Bad integer input, ignoring procedure call.');
WHEN no_such_id THEN
DBMS_OUTPUT.PUT_LINE('Bad Product id, ignoring procedure call.');
END;
/