I am trying to run a pl/sql procedure which I have attatched below, and am getting the following error when running my script
DBMS_OUTPUT.ENABLE;
create or replace procedure grade_point212
is
cursor c1 is
----Cursor declaration--
SELECT student.student_id, student.first_name, student.last_name, course.course_id, course.course_name, class.grade
FROM CLASS
JOIN STUDENT
ON student.student_id = class.student_id
JOIN course
ON course.course_id = class.course_id
order by 1;
----Variable declation--
v_student_id student.student_id%type;
v_first_name student.first_name%type;
v_last_name student.last_name%type;
v_course_id course.course_id%type;
v_course_name course.course_name%type;
v_grade class.grade%type;
--3 additional varriables--
prev_student student.student_id%type;
course_count number(3);
grade_point_total number(3);
----mainline--
begin
open c1;
loop
--Get the first row in the cursor--
fetch c1
into v_student_id,v_first_name ,v_last_name,v_course_id,v_course_name,v_grade;
exit when c1%notfound;
--Set the prev_student to the cursor’s student id--
prev_student:=v_student_id;
--Set the grade_point _total to 0--
grade_point_total:=0;
--Set the course_count to 0--
course_count:=0;
--If the prev_studentis NOT equal to cursor’s student id--
IF prev_student!=v_student_id THEN
--Print out the grade point average which is grade_point_total divided by course_count--
DBMS_OUTPUT.PUT_LINE(grade_point_total/course_count);
--Set prev_student to the cursor’s student id--
prev_student:=v_student_id;
--Set the grade_point_total to 0--
grade_point_total:=0;
--Set the course_count to 0--
course_count:=0;
END IF;
--Add the grade point of the cursor’s grade to grade_point_total--
grade_point_total:=grade_point_total+GradePoint(v_grade);
--Add 1 to the course_count--
course_count:=course_count+1;
--Print out the current row--
DBMS_OUTPUT.PUT_LINE(v_student_id||' '||v_first_name||' '||v_last_name||' '||v_course_id||' '||v_course_name||' '||v_grade);
--Fetch a new row--
fetch c1
into v_student_id,v_first_name ,v_last_name,v_course_id,v_course_name,v_grade;
end loop;
--Close the cursor--
close c1;
--Print out the grade point average which is grade_point_total divided by course_count--
DBMS_OUTPUT.PUT_LINE(grade_point_total/course_count);
end;
set serveroutput on;
begin
grade_point212;
end;
"Error starting at line : 1 in command -
DBMS_OUTPUT.ENABLE
Error report -
Unknown Command
Procedure GRADE_POINT212 compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
65/1 PLS-00103: Encountered the symbol "SET"
Errors: check compiler log
"
The "/" is correct. As a follow up, what tool you are using to send your scripts to the database?
SQL*Plus uses the standalone "/" line as the indicator that the sequence of strings is complete and therefore can be sent to the database for compilation and execution. If you don't provide the "/" you will get errors that on closer read will indicate that some compilation has failed because the sequence you send will actually be composed of multiple SQL, SQL*Plus, and PLSQL blocks.
The execute keyword, BTW, as in:
exec dbms_output.enable;
is SQLPlus syntactic sugar that gets converted by SQLPlus into:
begin dbms_output.enable; end;
Try exec in front of your dbms..
exec dbms_output.enable;
Remove the first line (DBMS_OUTPUT.ENABLE;) entirely. set serveroutput on (before you're about to execute the procedure) will enable it.
Or, encapsulate it into the procedure or the anonymous PL/SQL block itself, e.g.
SQL> begin
2 dbms_output.enable;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL>
You missed the trailing "/" to execute the "CREATE OR REPLACE" statement. The ";" As explained here, the semi-colon ends your pl/sql statement but you need the slash to execute it.
The error "Encountered the symbol "SET" Errors: check compiler log " refers to the "SET" string from the "set serveroutput on" statement.
...
DBMS_OUTPUT.PUT_LINE(grade_point_total/course_count);
end;
/ <<< Add this
set serveroutput on;
begin
grade_point212;
end;
/ <<< Add this
Related
I'm trying to add some ids into an array:
CREATE OR REPLACE TYPE array_of_numbers AS VARRAY(10)OF NUMBER(10);
declare
student_ids array_of_numbers;
begin
select nrleg BULK COLLECT into student_ids from student where nrleg = 123458;
FOR i IN 1..student_ids.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(student_ids(i));
END LOOP;
end;
but I get the following errors:
--------- -------------------------------------------------------------
3/1 PLS-00103: Encountered the symbol "DECLARE"
10/4 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << 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
Errors: check compiler log
Could someone explain what I did wrong?
Some DDL needs to be terminated with a /; a type can have a PL/SQL body so this is one of them. To run that all at once as a script do:
CREATE OR REPLACE TYPE array_of_numbers AS VARRAY(10)OF NUMBER(10);
/
declare
student_ids array_of_numbers;
begin
select nrleg BULK COLLECT into student_ids from student where nrleg = 123458;
FOR i IN 1..student_ids.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(student_ids(i));
END LOOP;
end;
/
You don't need the semicolon on the end of the CREATE here, but it doesn't hurt; but for some other commands (including other DDL like create table) having both would cause it to try to execute the statement twice, which could cause an error.
SQL Developer won't complain about the lack of a / after the last PL/SQL block in a script, but other tools will, so it's better to always include that one too.
db<>fiddle
Incidentally, another way to see the contents of the array in SQL Developer is with a ref cursor:
var rc refcursor
declare
student_ids array_of_numbers;
begin
select nrleg BULK COLLECT into student_ids from student where nrleg = 123458;
open :rc for select * from table(student_ids);
end;
/
print rc
... but then you might as well just select directly from the table, without any PL/SQL.
CREATE or REPLACE PROCEDURE SP_Projects is
Cursor C_proj is SELECT PROJECTID FROM PROJECT
ORDER BY PROJECTID;
R_proj C_proj%ROWTYPE;
BEGIN
OPEN c_proj;
LOOP
FETCH c_proj into r_proj;
EXIT when c_proj%NOTFOUND;
DBMS_OUTPUT.Put_line(c_proj.ProjectID);
END LOOP;
CLOSE c_proj;
END;
SET SERVEROUTPUT ON;
EXEC SP_Projects;
LINE/COL ERROR
17/1 PLS-00103: Encountered the symbol "SET"
Errors: check compiler log
You need to end procedure with slash /:
END;
/
I have to insert almost 40 000 rows to a table and every time I try to execute it in SQL developer, I couldn't find any rows in the table it seems like it didn't executed the begin block at all, because i put a msg to be printed in the output and there is nothing after the execution, I have tried to execute the block with only a few insertion statements and it works,
why SQL developer can't handle a lot of insertion statement in PLSQL block?
bellow is my script:
set serveroutput on
set linesize 200
set timing on
whenever sqlerror exit failure rollback;
whenever oserror exit failure rollback;
ALTER SESSION SET current_schema = carp;
ALTER SESSION SET nls_length_semantics = char;
DECLARE
v_rows_count VARCHAR2(10 CHAR);
PROCEDURE trace (p_message IN VARCHAR2)
IS
BEGIN
DBMS_OUTPUT.put_line (TO_CHAR (SYSDATE, 'YYYYMMDD HH24:MI:SS') || ' - INFO - ' || p_message);
END trace;
BEGIN
dbms_output.ENABLE(1000000);
TRACE ('=========== Exécution du script: START ===========');
Insert into my_table (PK_SEQ,CODE) values (1260389,'00AI');
Insert into my_table (PK_SEQ,CODE) values (1321244,'00AI');
select COUNT(*) into v_rows_count FROM carp.param_site_groupe_site;
dbms_output.put_line('Nombre de lignes insérées: ' || v_rows_count);
TRACE (q'[=========== Exécution du script: FIN ===========]');
COMMIT;
END;
/
exit;
please note that there is more than 40 000 insert statements inside the begin block not two as shown above
I reduce the script size just for readebility porpuse
I have oracle database 11gR2 and i want to create some triggers automatically with a block .
when i want to run query in below , i have an error .
My block is :
declare
tablesid number ;
tablenames varchar2(4000);
cursor c1 is
select id , title from hr.tables;
begin
open c1;
loop
fetch c1 into tablesid , tablenames;
CREATE OR REPLACE TRIGGER tablenames||'_before_insert'
before insert
on hr.tablenames
DECLARE
columnid number ;
columnval number ;
columntitle varchar(4000);
cursor c2 is
select id from hr.columns where tableid = tablesid ;
BEGIN
-- open c2 ;
for rec in c2 loop
select title into columntitle from hr.columns where id = rec.id
insert into hr.rowaction(columnid,newvalue,oldvalue,actiondate)
(
select id ,:new.columntitle,:old.columntitle,sysdate
from hr.tabletiltes
where id = rec.id
)
select title into columntitle from hr.columns where id = rec.id;
dbms_output.put_line(columntitle);
end loop;
end;
end loop ;close c1; end;
ORA-06550: line 11, column 6:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare end 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
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action :
Bind Variable "new" is NOT DECLARED
anonymous block completed
Bind Variable "new" is NOT DECLARED
anonymous block completed
Thanks
A trigger is a standalone separate piece of code that is usually considered DDL, the language that defines metadata for objects.
You cannot have a trigger declaration embedded inside a PL/SQL block. IF you need to CREATE some PL/SQL code on the fly - not a great idea - consider the DMBS_DDL.CREATE_WRAPPED procedure. You seem to have EXECUTE IMMEDIATE in mind as well. If so, have a read on this: (http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_ddl.htm)
You should define your trigger BEFORE running your PL/SQL in other words. Make two scripts.
This question already has answers here:
Printing the value of a variable in SQL Developer
(9 answers)
Closed 4 years ago.
I'm just getting into PL/SQL, and I tried to run the following code, and I am getting anonymous block completed, but I think I should be getting Testing output. Does any know what I am doing wrong?
DECLARE
message varchar2(20) := 'Testing output';
BEGIN
dbms_output.put_line(message);
END;
/
Viewing the DBMS_OUTPUT depends on the program.
SQL*Plus and Oracle SQL Developer
Run SET SERVEROUTPUT ON; first. This is all that's necessary in SQL*Plus or recent versions of Oracle SQL Developer.
SET SERVEROUTPUT ON;
begin
dbms_output.put_line('Testing output');
end;
/
PL/SQL Developer
Output is automatically detected and displayed in the "Output" tab.
Yes, in Oracle SQL Developer put the statement:
SET SERVEROUTPUT ON;
just before your DECLARE keyword and this should work.
I couldn't find View -> DBMS Output and I'm using version 1.5.5.
Yes. There is way to see output in SQL Developer.
Click ->View->Dbms Output and then click + symbol on Dbms output window. now you can run the procedure and can see output.
`The following statement will give the possible solution try this out
SET SERVEROUTPUT ON;
Then Run this code will get the following output
declare
a integer :=10;
b integer :=20;
c integer;
f real;
begin
c := a+b;
dbms_output.put_line('value of c: ' || c);
f := 70.0/3.0;
dbms_output.put_line('value of f: ' || f);
end;
/
The code will give the following output
value of c: 30
value of f: 23.3333333333333333333333333333333333333
PL/SQL procedure successfully completed.
Yes, this is correct. You need to use before this block:
SET SERVEROUTPUT ON
Then, message get displayed on window.
Else we can check in SQL Developer select "View" -> "DBMS Output".
and in PLSQL developer under the OutPut tab we can check message.
If you are getting "anonymous block completed" while executing the procedure by typing "EXECUTE ;" then run the below command & again execute the procedure.
command is
SET SERVEROUTPUT ON;
**SET SERVEROUTPUT ON;**
DECLARE
a INTEGER :=10;
b INTEGER :=20;
c float ;
d real ;
BEGIN
c :=a+b;
dbms_output.put_line('the value of C is :'|| c);
d := 70.0/3.3;
dbms_output.put_line('the value of d is:'|| d);
END;
This will give you the output
the value of C is: 30
the value of d is: 21.21212121212121212121212121212121212121