A frustrating PL/SQL Error...PLS-00103 - sql

I have been learning PL/SQL for one of my classes and I am having a difficult time debugging the PLS-00103 error. I have searched the error online, but the solutions I have found aren't necessarily specific enough to solve my problem. Maybe I just need a few more eyes on this. Below you will find my code and the corresponding error message. Thank you in advance! The sample output is below the error.
Code:
create or replace procedure pro_AvgGrade as
min_avg NUMBER := 100;
max_avg NUMBER := 0;
bins NUMBER := 0;
binlen NUMBER := 10;
temp NUMBER := 0;
binprint NUMBER := 0;
CURSOR grades IS SELECT AvgGrade FROM (SELECT DeptName, AVG(GradeAsPercentage) AS AvgGrade FROM Department JOIN Course on Department.DeptId = Course.DeptId JOIN Offering on Course.CourseId = Offering.CourseId JOIN Registration on Offering.OfferingId = Registration.OfferingId GROUP BY DeptName ORDER BY DeptName ASC);
grade_tuple grades%ROWTYPE;
BEGIN
open grades;
for grade_tuple in grades loop
temp := grade_tuple.AvgGrade;
if (temp>max) then
max_avg := temp;
elsif (temp<min) then
min_avg := temp;
end if;
end loop;
close grades;
binprint := min_avg;
bins := max_avg-min_avg;
bins := bins/binlen;
dbms_output.put_line('DeptName AvgGrade: ');
LOOP
dbms_output.put('>' || min_avg || ',' || '<=');
min_avg := binlen+10;
dbms_output.put(min_avg);
dbms_output.put(' ');
i := i+1;
EXIT WHEN i>bins;
END LOOP;
END pro_AvgGrade;
/
begin
pro_AvgGrade;
end;
/
Error:
LINE/COL ERROR
-------- -----------------------------------------------------------------
19/15 PLS-00103: Encountered the symbol ")" when expecting one of the
following:
(
21/18 PLS-00103: Encountered the symbol ")" when expecting one of the
following:
(
Sample Output:
DEPTNAME AVGGRADE: >70, <=80 >80,<=90

May be it's because max and min are reserved words...so it looking for max() and min()...
But it looks more that you just made a mistake. I think you need to try this :
if (temp>max_avg) then
max_avg := temp;
elsif (temp<min_avg) then
min_avg := temp;
end if;

There is no need for parenthesis in your if block. Write it like this:
if temp>max then
max_avg := temp;
elsif temp<min then
min_avg := temp;
end if;

Related

how to find multiple of a number using goto sql statement?

Write a PL/SQL block to display the multiples of a Given Number
without including multiples of 10 for a given range (Start Value and
End Value), using GOTO.
This is my try, but I couldn't place the goto statement in correct place:
declare
start_value number; end_value number; n number;
result;
begin
start_value:=&start_value;
end_value:=&end_value;
n :=&n;
for x in start_value..end_value loop
<<calc>>
if((x mod n)=0 and (x mod 10)!=0) then
dbms_output.put_line(x);
end if;
goto calc;
end loop;
end;
First off, this is a really bad assignment. Teaching you to use GOTO's is like teaching a naval architect to build ships without watertight bulkheads - it's Just Wrong. (sigh) But, oh well...
DECLARE
start_value NUMBER;
end_value NUMBER;
n NUMBER;
BEGIN
start_value := &start_value;
end_value := &end_value;
n := &n;
FOR x IN start_value..end_value LOOP
IF MOD(x, 10) = 0 THEN
GOTO skip_calc;
END IF;
DBMS_OUTPUT.PUT_LINE(n * x);
<<skip_calc>>
NULL;
END LOOP;
END;
The way this would normally be written is:
DECLARE
start_value NUMBER;
end_value NUMBER;
n NUMBER;
BEGIN
start_value := &start_value;
end_value := &end_value;
n := &n;
FOR x IN start_value..end_value LOOP
IF MOD(x, 10) <> 0 THEN
DBMS_OUTPUT.PUT_LINE(n * x);
END IF;
END LOOP;
END;
Shorter and easier to read.

PLS-00306: when calling a function from a PL/SQL block

I am getting the PLS-00306 error when I attempt to run a PL/SQL block that calls a variety of things including a function. The job of the function is to count how many cars belong to a certain model type. The function works if I call it in a SQL statement or it's own block, it just doesn't seem to work here.
This is the function:
CREATE OR REPLACE Function findtotalcarmodels(
model_name_in IN varchar2)
RETURN NUMBER
IS
counter NUMBER := 0;
CURSOR car_count_cur IS
SELECT model_name
FROM i_car
WHERE model_name = model_name_in;
Rec_car_details car_count_cur%ROWTYPE;
BEGIN
OPEN car_count_cur;
LOOP
FETCH car_count_cur INTO Rec_car_details;
EXIT WHEN car_count_cur%NOTFOUND;
counter := counter + 1;
END LOOP;
CLOSE car_count_cur;
RETURN counter;
END;
This is the Block:
SET SERVEROUTPUT ON FORMAT WRAP SIZE 12000
Declare
v_model VARCHAR2(40);
v_carcategory VARCHAR2(40);
v_totalcars NUMBER;
v_maxdate DATE:=TO_DATE(1, 'J');
Cursor carcur IS
SELECT *
FROM i_car;
CURSOR c1(v_car_registration VARCHAR2) IS
SELECT *
from i_booking a
WHERE a.registration=v_car_registration;
Begin
For car_rec in carcur
LOOP
v_maxdate:=TO_DATE(1, 'J');
for rec in c1(car_rec.registration)
loop
IF rec.date_reserved > v_maxdate
then
v_maxdate:=rec.date_reserved ;
If car_rec.Cost <=50000
THEN
v_carcategory := 'Budget Car';
End IF;
If car_rec.Cost BETWEEN 50000 AND 100000
THEN
v_carcategory := 'Standard Car';
End IF;
If car_rec.Cost >100000
THEN
v_carcategory := 'Premium Car';
End If;
end IF;
v_totalcars := findtotalcarmodels;
end loop;
DBMS_OUTPUT.PUT_LINE('Registration:'|| ' '|| car_rec.registration);
DBMS_OUTPUT.PUT_LINE('Cost:'|| ' $' || car_rec.Cost);
DBMS_OUTPUT.PUT_LINE('Model Name:'|| ' '|| car_rec.model_name);
DBMS_OUTPUT.PUT_LINE('Car Category:'|| ' '||v_carcategory);
DBMS_OUTPUT.PUT_LINE('Total number of Cars:'|| ' '||v_totalcars);
DBMS_OUTPUT.PUT_LINE('Most Recent Rental Date: '|| ' '||v_maxdate);
DBMS_OUTPUT.NEW_LINE;
END LOOP;
END;
/
Before I get slammed for the style of the PL/SQL block, just keep in mind that it is written to requirement and everything works well with the exception of the function.
If someone could point me in the right direction to call this function without error I would be very grateful.
It looks like you have missed to pass the IN parameter to the function.
Try like this,
v_totalcars := findtotalcarmodels('<model_name_in>');
Well, when i see things right, you don't fillup the parameter model_name. When you don't have an overriden function wihtout parameter you need to fill it up.
AS you can also see the PLS-00306 is telling you something about wrong number of arguments.

how can we get the value of of VARRAY from IN Parameter in procedure

I am new to PL/SQL... In Varray how can i get multiple value from IN parameter..... else Is there another ways to get the values...
I want to interate the values through VArray... if any other options then its fine..
coding:
CREATE OR REPLACE
PROCEDURE dynamic_query_build(
vr_plan_sku_id IN VARCHAR2 )
IS
type plan_sku_id_array IS VARRAY(999) OF VARCHAR2(5000);
plan_sku_id plan_sku_id_array;
total INTEGER;
vrx_plan_sku_id VARCHAR2(3000);
BEGIN
vrx_plan_sku_id:= REPLACE(vr_plan_sku_id,',',chr(39)||','||chr(39));
plan_sku_id := plan_sku_id_array(chr(39)||vrx_plan_sku_id||chr(39));
total := plan_sku_id.count;
FOR i IN 1 .. total
LOOP
dbms_output.put_line(plan_sku_id(i));
END LOOP;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END dynamic_query_build;
Execution:
set serveroutput on;
declare
vr_plan_sku_id varchar2(200) := '5863314,5863315';
BEGIN
dynamic_query_build(vr_plan_sku_id);
END;
/
My Output:
anonymous block completed
'5863314','5863315'
Expected output:
5863314
5863315
now it is considering as single value....
I created anonymous block with procedure dynamic_query_build. Added there code, that will split VARCHAR2 variable into varray.
I think, the key to your question is this line - plan_sku_id.EXTEND();
You can extend varray dynamically, but only till it reaches defined maximum (in your case - 999).
DECLARE
vr_plan_sku_id varchar2(200) := '5863314,5863315';
PROCEDURE dynamic_query_build(
vr_plan_sku_id IN VARCHAR2 )
IS
type plan_sku_id_array IS VARRAY(999) OF VARCHAR2(5000);
plan_sku_id plan_sku_id_array;
total INTEGER;
position PLS_INTEGER := 0;
last_position PLS_INTEGER := 1;
tmp VARCHAR2(5000);
counter PLS_INTEGER := 1;
BEGIN
plan_sku_id := plan_sku_id_array();
LOOP
position := INSTR(vr_plan_sku_id, ',', last_position);
IF position > 0 THEN
tmp := SUBSTR(vr_plan_sku_id, last_position, position - last_position);
last_position := position + 1;
ELSE
tmp := SUBSTR(vr_plan_sku_id, last_position);
END IF;
plan_sku_id.EXTEND();
plan_sku_id(counter) := tmp;
counter := counter + 1;
EXIT WHEN position = 0 OR counter > 10;
END LOOP;
total := plan_sku_id.count;
FOR i IN 1 .. total
LOOP
dbms_output.put_line(plan_sku_id(i));
END LOOP;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END dynamic_query_build;
BEGIN
dynamic_query_build(vr_plan_sku_id);
END;
/
Put a replace in the dbms_output statement this will eliminate the quotes from the string
....
dbms_output.put_line replace (replace (plan_sku_id(i), '''' ))',',chr(10);
.....

String Split/ String replace based on character length

Here my problem in a notes column having 2000 characters max, i want my string output based on 35 characters, ya i need to replace <br> tag after 30 characters in the string.. example string "hi hello how are you doing out there, need your help!", i need out put as "hi hello how are you doing out<br> there, need your help! similar i need to calculate the sting length and have to split it 35+35+35.. i don't know how to perform this in sql/plsql.
select substr(note,1,(instr(note, ' ',35)))||'<br>'||substr(note,instr(note, ' ',35),
(instr(note, ' ',35)))notes from test
DECLARE
CURSOR notes_cur IS
SELECT 1 note_id, 'hi hello how are you doing out there, need your help! hi hello how are you doing out there, need your help!' note FROM DUAL UNION ALL
SELECT 2, 'hi hello how are you doing out there, need your help! hi hello how are you doing out there, need your help!' note FROM DUAL;
TYPE notes_ntt IS TABLE OF notes_cur%ROWTYPE;
l_notes notes_ntt;
l_loop_counter NUMBER;
l_split notes_ntt := notes_ntt();
l_space_start NUMBER;
l_string_start NUMBER;
l_space_position NUMBER;
BEGIN
OPEN notes_cur;
FETCH notes_cur BULK COLLECT INTO l_notes;
CLOSE notes_cur;
FOR indx IN 1..l_notes.COUNT LOOP
l_space_start := 33;
l_string_start := 1;
l_loop_counter := TRUNC(LENGTH(l_notes(indx).note) / 35);
FOR note IN 1..l_loop_counter LOOP
l_split.EXTEND;
l_split(l_split.LAST).note_id := l_notes(indx).note_id;
l_space_position := INSTR(l_notes(indx).note, CHR(32), l_space_start, 1);
l_split(l_split.LAST).note := SUBSTR
(
l_notes(indx).note
, l_string_start
, CASE
WHEN l_space_position = 0
THEN l_string_start
ELSE l_space_position - l_string_start
END
) || CHR(10);
l_space_start := l_space_position + 33;
l_string_start := l_space_position + 1;
END LOOP;
END LOOP;
FOR indx IN 1..l_split.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(l_split(indx).note_id || ' ' || l_split(indx).note);
NULL;
END LOOP;
END;
/*
1 hi hello how are you doing out there,
1 need your help! hi hello how are
1 you doing out there, need your help!
2 hi hello how are you doing out there,
2 need your help! hi hello how are
2 you doing out there, need your help!
*/
You could do this:
declare
l_in_string varchar2(1000) := 'hi hello how are you doing out there, need your help!';
l_out_string varchar2(1000);
begin
while length(l_in_string) > 35 loop
l_out_string := l_out_string || substr(l_in_string, 1, 35) || '<br>';
l_in_string := substr(l_in_string, 36);
end loop;
l_out_string := l_out_string || l_in_string;
dbms_output.put_line(l_out_string);
end;
However this is quite likely to break mid-word e.g.
hi hello how are you doing out there, need your help!
You would need to write more sophisticated code if you want to break on spaces only.

Oracle : how to fetch data from dynamic query?

I have a program to generate dynamic query string based on input. This query may select from any tables or joined tables in my DB, and the column names and number of columns are unknown.
Now with this query string as the only input, I want to fetch all data from the result and output them line by line, is there any way to do this ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thank Thinkjet for the reference. I have solved the problem, to help the others, here is the piece of code I used:
DECLARE
v_curid NUMBER;
v_desctab DBMS_SQL.DESC_TAB;
v_colcnt NUMBER;
v_name_var VARCHAR2(10000);
v_num_var NUMBER;
v_date_var DATE;
v_row_num NUMBER;
p_sql_stmt VARCHAR2(1000);
BEGIN
v_curid := DBMS_SQL.OPEN_CURSOR;
p_sql_stmt :='SELECT * FROM emp';
DBMS_SQL.PARSE(v_curid, p_sql_stmt, DBMS_SQL.NATIVE);
DBMS_SQL.DESCRIBE_COLUMNS(v_curid, v_colcnt, v_desctab);
-- Define columns:
FOR i IN 1 .. v_colcnt LOOP
IF v_desctab(i).col_type = 2 THEN
DBMS_SQL.DEFINE_COLUMN(v_curid, i, v_num_var);
ELSIF v_desctab(i).col_type = 12 THEN
DBMS_SQL.DEFINE_COLUMN(v_curid, i, v_date_var);
ELSE
DBMS_SQL.DEFINE_COLUMN(v_curid, i, v_name_var, 50);
END IF;
END LOOP;
v_row_num := dbms_sql.execute(v_curid);
-- Fetch rows with DBMS_SQL package:
WHILE DBMS_SQL.FETCH_ROWS(v_curid) > 0 LOOP
FOR i IN 1 .. v_colcnt LOOP
IF (v_desctab(i).col_type = 1) THEN
DBMS_SQL.COLUMN_VALUE(v_curid, i, v_name_var);
ELSIF (v_desctab(i).col_type = 2) THEN
DBMS_SQL.COLUMN_VALUE(v_curid, i, v_num_var);
ELSIF (v_desctab(i).col_type = 12) THEN
DBMS_SQL.COLUMN_VALUE(v_curid, i, v_date_var);
END IF;
END LOOP;
END LOOP;
DBMS_SQL.CLOSE_CURSOR(v_curid);
END;
/
You can do that with DBMS_SQL package.
Update
To get more detailed reference about DBMS_SQL go here.
If you are building your string within PL/SQL, you can run it with EXECUTE IMMEDIATE. <- link. Use the BULK COLLECT INTO and output the collection.
<PRE>
DECLARE
RUN_S CLOB;
IGNORE NUMBER;
SOURCE_CURSOR NUMBER;
PWFIELD_COUNT NUMBER DEFAULT 0;
L_DESCTBL DBMS_SQL.DESC_TAB2;
Z_NUMBER NUMBER;
BEGIN
RUN_S := ' SELECT 1 AS VAL1,
2 AS VAL2,
CURSOR (SELECT 11 AS VAL11,
12 AS VAL12
FROM DUAL) AS CUR1,
CURSOR (SELECT 11 AS VAL11,
12 AS VAL12
FROM DUAL) AS CUR2
FROM DUAL';
SOURCE_CURSOR := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(SOURCE_CURSOR, RUN_S, DBMS_SQL.NATIVE);
DBMS_SQL.DESCRIBE_COLUMNS2(SOURCE_CURSOR, PWFIELD_COUNT, L_DESCTBL); -- get record structure
FOR I IN 1 .. PWFIELD_COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Col ' || I || ' Type:' || L_DESCTBL(I).COL_TYPE);
IF L_DESCTBL(I).COL_TYPE = 2 THEN
DBMS_SQL.DEFINE_COLUMN(SOURCE_CURSOR, I, Z_NUMBER);
END IF;
NULL;
END LOOP;
IGNORE := DBMS_SQL.EXECUTE(SOURCE_CURSOR);
LOOP
IF DBMS_SQL.FETCH_ROWS(SOURCE_CURSOR) > 0 THEN
FOR I IN 1 .. PWFIELD_COUNT LOOP
IF L_DESCTBL(I).COL_TYPE IN (2) THEN
DBMS_SQL.COLUMN_VALUE(SOURCE_CURSOR, I, Z_NUMBER);
DBMS_OUTPUT.PUT_LINE('Col ' || I || ' Value:' || Z_NUMBER);
END IF;
END LOOP;
ELSE
EXIT;
END IF;
END LOOP;
END;
</PRE>