Trying to create stored procedure in Oracle SQL - sql

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.

Related

How to write a procedure to square a number in oracledb

CREATE OR REPLACE PROCEDURE sqr(n IN NUMBER, res OUT NUMBER)
IS
BEGIN
res:= n*n;
Dbms_Output.PUT_LINE(res);
Dbms_Output.PUT_LINE('PL/SQL procedure successfully completed');
END;
/
DECLARE
ans NUMBER;
BEGIN
EXECUTE sqr(15, :ans);
END;
/
I'm new to SQL, trying to create a query to square a number using procedure i dont know how to do it. i was able to come with code above but for some reason i cant connect to DB!
Help needed. (ignore if there are any mistakes in my que framing)
Code you wrote is ... well, not that bad. With some changes, it works:
SQL> SET SERVEROUTPUT ON
SQL> CREATE OR REPLACE PROCEDURE sqr(n IN NUMBER, res OUT NUMBER)
2 IS
3 BEGIN
4 res:= n*n;
5 END;
6 /
Procedure created.
SQL> DECLARE
2 ans NUMBER;
3 BEGIN
4 sqr(15, ans);
5 Dbms_Output.PUT_LINE(ans);
6 END;
7 /
225
PL/SQL procedure successfully completed.
Though, you'd rather create a function (instead of a procedure):
SQL> create or replace function f_sqr (n in number)
2 return number
3 is
4 begin
5 return n*n;
6 end;
7 /
Function created.
SQL> select f_sqr(15) ans from dual;
ANS
----------
225
SQL>
But, nothing of that matters because you said
for some reason i cant connect to DB!
That's the real problem, and you shared absolutely no information about it. Which tool did you use? Do you have a database at all? Where is it? On your own computer or on a server (available to you through the network)? How exactly did you try to connect? What did Oracle say?

PLS-00103: Encountered the symbol "&" when expecting one of the following

PLS-00103: Encountered the symbol "&" when expecting one of the following...
code:
Declare
num number;
Begin
num := &num;
if num > 0 then
dbms_output.put_line(‘Hello’);
end if;
end;
/
I'm not sure why I'm getting this message when I'm not seeing anything wrong.
Oracle Live SQL is a tool for trying out SQL and PL/SQL but it doesn't support substitution variable syntax (&var.).
Instead, you can create tables, populate them with data, then run SQL or PL/SQL using them, e.g.:
create table inputs (num number);
insert into inputs values (10);
Declare
num number;
Begin
select num into num from inputs;
if num > 0 then
dbms_output.put_line('Hello');
end if;
end;
/
(p.s. I had to fix the quotes around 'Hello' for this to work)
Oracle live does not support & for get value.
Instead you can initialize the Variable
Declare
num number := 10;
Begin
if num > 0 then
dbms_output.put_line(‘Hello’);
end if;
end;
/

stored procedure in oracle 11g

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

Remove special Character from complete table

I have 5 to 6 tables in which i am inserting data using Excel file but i don't know whats the issue but for some data special character are adding and that is giving me problem. So is their any way to update complete table by passing the only Table_name in some procedure.currently i am using one function to update the column but i want something more easy to use (for my other team member).
I am using below function .
create or replace FUNCTION pfm_on_varchar(
p_str IN VARCHAR2)
RETURN VARCHAR2
IS
o_str VARCHAR2(4096) := '';
asc_val NUMBER;
BEGIN
FOR I IN 1 .. LENGTH(p_str)
LOOP
asc_val := ascii(SUBSTR(p_str,i,1));
IF ((asc_val BETWEEN 48 AND 57) OR (asc_val BETWEEN 65 AND 90) OR (asc_val BETWEEN 97 AND 122)) THEN
o_str := o_str || chr(asc_val);
END IF;
END LOOP;
RETURN o_str;
END pfm_on_varchar;
and how can I pass table name and column name in a procedure to update the table ?I am trying this code
CREATE OR REPLACE PROCEDURE removeSpecialChar(table_new IN varchar2, column_new IN varchar2)
AS
BEGIN
update table_new
set column_new = PFM_ON_VARCHAR(column_new);
end removeSpecialChar;
/
Error I am getting
Error(4,3): PL/SQL: SQL Statement ignored
Error(4,10): PL/SQL: ORA-00942: table or view does not exist
As mentioned in the comments , you can try using SQL as below:
Update table1
set columnname = case
when ascii(columnname) between 48 and 54
then replace (columnname,ascii(48),'')
end
where <condition> ;
update table
set columnName = regexp_replace('This is a test. $%&^*&* ', '[^A-Za-z .?!]', '')
Add all the characters which you don't want removed in the [^A-Za-z .]
For the procedure you could do a dynamic sql. Otherwise it will not pick the variable up. Like this:
CREATE OR REPLACE PROCEDURE removeSpecialChar(table_new IN varchar2, column_new IN varchar2)
AS
BEGIN
execute immediate 'update '||table_new||
' set '||column_new||' = PFM_ON_VARCHAR('||column_new||')';
end removeSpecialChar;
/

Displaying data through stored procedure

Create or replace procedure disp(pEMPLASTNAME varchar2)
IS
Row employee%rowtype;
begin
select * into row from employee where EMPLASTNAME=’pEMPLASTNAME’ ;
dbms_output.put_line('Name: '||Row.EMPID||' '|| Row.EMPNAME);
End;
/
BEGIN
disp(‘Mark’);
END;
/
Hello, I am trying to display data from a a table using stored procedures. The last name is passed as a parameter through the stored procedure and upon execution , the stored procedure should display all the rows which have the last name . Here is the error that i get; pls help! :-
SQL> BEGIN
disp('Mark');
END;
/
BEGIN
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "TEST.DISP", line 5
ORA-06512: at line 2
No need of the quotes:
select * into row from employee where EMPLASTNAME=pEMPLASTNAME;
However, you may not have any data for that variable's value anyway (i.e. one day, it may happen)
That's why I recommend you catch the exception and treat it (see EXCEPTION block)
pd: it is a good practice to not use reserved words such as row. I recommend you name your variable another way.
I would suggest using a cursor to select all rows and then looping through the cursor to print the result:
Create or replace procedure disp(pEMPLASTNAME varchar2)
IS
Cursor row_select is
select EMPID, EMPNAME from employee where emplastname = pEMPLASTNAME;
-- and whatever columns you need to print, using * isn't good practice
begin
for item in row_select loop
dbms_output.put_line('Name: '||item.EMPID||' '|| item.EMPNAME);
end loop;
End;
/
BEGIN
disp(‘Mark’);
END;
/