Getting this error while displaying even semesters - mysql

Want to display even semesters but getting this error.It seems ok but am getting exception please correct me.
declare
cursor scursor is select rno,sname,sem from student;
srec scursor%rowtype;
begin
for srec in scursor
loop
continue when (mod(srec.sem,2)!=0);
dbms_output.put_line('Student name '||srec.sname);
dbms_output.put_line('Roll no '||srec.rno);
dbms_output.put_line('Semester '||srec.sem);
end loop;
end;
Exception
ERROR at line 7:
ORA-06550: line 7, column 11:
PLS-00103:
Encountered the symbol "WHEN" when expecting one of the following:
:= . ( # % ;

Why not just use a select instead of a cursor?
In any case, put the logic in the query itself:
declare cursor scursor is
select rno, sname, sem
from student
where mod(sem, 2) = 0;
srec scursor%rowtype;
This is also more efficient.

You would need to use the right syntax of CONTINUE in the loop
IF mod(srec.sem,2)!=0 THEN
CONTINUE;
END IF;

Related

How to write plsql exception satatement to ask user to give input in specific format

CREATE OR REPLACE PROCEDURE get_productdetails(
p_reqid OUT requirement.req_id%type,
p_pid OUT requirement.p_id%type,
p_rstaffid OUT requirement.r_staff_id%type,
p_demand requirement.demand%type)
IS
CURSOR c_demand IS
SELECT req_id,p_id,r_staff_id from requirement where demand = upper(p_demand);
BEGIN
FOR i in c_demand Loop
DBMS_OUTPUT.PUT_LINE('Requirement ID :'||i.req_id);
DBMS_OUTPUT.PUT_LINE('Product ID'||i.p_id);
DBMS_OUTPUT.PUT_LINE('Staff ID :'||i.r_staff_id);
END LOOP;
END get_productdetails;
User must enter demand in 'HIGH/LOW/AVG only otherwise it should throw exception asking to enter data in that format
Can you please help me to write an exception accordingly
As I don't have access to your table structure, the code below is untested. A good place to start is the official oracle documentation. In your case, a user-defined exception is needed, which you need to declare in the declaration section, then RAISE and finally catch in the EXCEPTION block.
CREATE OR REPLACE PROCEDURE get_productdetails(
p_reqid OUT requirement.req_id%type,
p_pid OUT requirement.p_id%type,
p_rstaffid OUT requirement.r_staff_id%type,
p_demand requirement.demand%type)
IS
-- declare the user-defined exception
invalid_input EXCEPTION;
CURSOR c_demand IS
SELECT req_id,p_id,r_staff_id from requirement where demand = upper(p_demand);
BEGIN
IF p_demand NOT IN ('HIGH','LOW','AVG') THEN
-- raise the exception if p_demand not one of the 3 accepted values
RAISE invalid_input;
END IF;
FOR i in c_demand Loop
DBMS_OUTPUT.PUT_LINE('Requirement ID :'||i.req_id);
DBMS_OUTPUT.PUT_LINE('Product ID'||i.p_id);
DBMS_OUTPUT.PUT_LINE('Staff ID :'||i.r_staff_id);
END LOOP;
EXCEPTION WHEN invalid_input THEN
dbms_output.put_line('Only HIGH/LOW/AVG are valid input');
END get_productdetails;

MySQL after update trigger using CONCAT_WS: Why is it inserting a newline where I DON'T want it?

The intended result is to store the notes of edits to a field, in another field.
I want the new notes to APPEND to the storage field, and since the is not function that does this I am attmpting to find a way to work this out without adding more layers of code like functions and stored procedures.
/* Before Update Trigger */
DECLARE v_description VARCHAR(255);
DECLARE v_permnotes MEDIUMTEXT;
DECLARE v_oldnote VARCHAR(500);
DECLARE v_now VARCHAR(25);
SET v_now = TRIM(DATE_FORMAT(NOW(), '%Y-%m-%d %k:%i:%s'));
SET v_oldnote = OLD.notes;
IF (NEW.permanent_notes IS NULL) THEN
SET v_permnotes = '';
ELSE
SET v_permnotes = OLD.permanent_notes;
END IF;
SET NEW.permanent_notes = CONCAT_WS(CHAR(10), v_permnotes, v_now,": ", v_description);
I'm aiming to have the results in the permanent field look like this
<datetime value>: Some annotation from the notes field.
<a different datetime>: A new annotation
etc....
What I get from my current trigger:
2018-12-30 17:15:50
:
Test 17: Start from scratch.
2018-12-30 17:35:51
:
Test 18: Used DATE_FORMAT to sxet the time
2018-12-30 17:45:52
:
Test 19. Still doing a carriage return after date and after ':'
I can't figure out why there is a newline after the date, and then again after the ':'.
If I leave out CHAR(10), I get:
Test 17: Start from scratch.
2018-12-30 17:35:51
:
Test 18: Used DATE_FORMAT to sxet the time
2018-12-30 17:45:52
:
Test 19. Still doing a carriage return after date and after ':'Test 20. Still doing a carriage return after date and after ':'
Some fresh/more experienced eyes would be really helpful in debugging this.
Thanks.
I think you should just be using plain CONCAT here:
DECLARE separator VARCHAR(1);
IF (NEW.permanent_notes IS NULL) THEN
SET separator = '';
ELSE
SET separator = CHAR(10)
END IF;
-- the rest of your code as is
SET
NEW.permanent_notes = CONCAT(v_permnotes, separator, v_now, ": ", v_description);
The logic here is that we conditionally print a newline (CHAR(10)) before each new log line, so long as that line is not the very first. You don't really want CONCAT_WS here, which is mainly for adding a separator in between multiple terms. You only want a single newline in between each logging statement.

Using BULK COLLECT with LIMIT clause

I am a beginner....just want to know why this following script is giving error while compiling.your replies will be helpful
create or replace procedure fetch_employee
is
cursor test_departments_cur
is
select * from test_departments;
Type test_departments_aat is
table of test_departments_cur%ROWTYPE
INDEX BY PLS_INTEGER;
l_test_departments test_departments_aat;
Begin
open test_departments_cur;
loop
fetch test_departments_cur
bulk collect into l_test_departments limit 10;
exit when l_test_departments.count=0;
for i in 1..l_test_departments.count
loop
dbms_output.put_line(l_test_departments(i));
end loop;
dbms_output.put_line('=============================================');
end loop;
close test_departments_cur;
end fetch_employee;
Its showing error: Error(23,5): PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
in
dbms_output.put_line(l_test_departments(i));
Please answer
"l_test_departments" is an array of type "test_departments_aat" which is holding a record. As its type is "test_departments_cur%rowtype" , so to access a cursor's value ,you need to pass the column of the cursor as follows :
dbms_output.put_line(l_test_departments(i).<column_name>);
Hope this helps.

Loop syntax error in MySQL cursor

I have been trying to use a cursor and following a tutorial I found online, I was able to come up with the following cursor.
DELIMITER $$
CREATE PROCEDURE customers_with_oldest_version (INOUT customerCount varchar(4000))
BEGIN
DEClARE customers_with_oldest_version CURSOR FOR
select * from CustomerSoftware where software in (select min(minimumSoftware) from ProductSoftware);
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET #finished = 1;
set #row_entry = "";
open customers_with_oldest_version;
get_customers: LOOP
FETCH customers_with_oldest_version INTO #row_entry;
IF #finished = 1 THEN
LEAVE get_customers;
END IF;
SET #customerCount = #customerCount + 1;
END LOOP;
CLOSE customers_with_oldest_version;
END$$
DELIMITER ;
But I am unable to create this procedure, since phpmyadmin is giving me an error saying that
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '#row_entry; IF #finished = 1 THEN LEAVE get_customers; END IF; SET #c' at line 16
What am I missing here?
You can't fetch into a user-defined variable. This is a bug that has been around for many years, see https://bugs.mysql.com/bug.php?id=2261
Declare a regular local variable for it.

function return varray error

I keep getting a error when i run this code, What wrong with the code?
create or replace function f_vars(line varchar2,delimit varchar2 default ',')
return line_type is type line_type is varray(1000) of varchar2(3000);
sline varchar2 (3000);
line_var line_type;
pos number;
begin
sline := line;
for i in 1 .. lenght(sline)
loop
pos := instr(sline,delimit,1,1);
if pos =0 then
line_var(i):=sline;
exit;
endif;
string:=substr(sline,1,pos-1);
line_var(i):=string;
sline := substr(sline,pos+1,length(sline));
end loop;
return line_var;
end;
LINE/COL ERROR
20/5 PLS-00103: Encountered the symbol "LOOP" when expecting one of
the following:
if
22/4 PLS-00103: Encountered the symbol "end-of-file" when expecting
one of the following:
end not pragma final instantiable order overriding static
member constructor map
Stack Overflow isn't really a de-bugging service.
However, I'm feeling generous.
You have spelt length incorrectly; correcting this should fix your first error. Your second is caused by endif;, no space, which means that the if statement has no terminator.
This will not correct all your errors. For instance, you're assigning something to the undefined (and unnecessary) variable string.
I do have more to say though...
I cannot over-emphasise the importance of code-style and whitespace. Your code is fairly unreadable. While this may not matter to you now it will matter to someone else coming to the code in 6 months time. It will probably matter to you in 6 months time when you're trying to work out what you wrote.
Secondly, I cannot over-emphasise the importance of comments. For exactly the same reasons as whitespace, comments are a very important part of understanding how something works.
Thirdly, always explicitly name your function when ending it. It makes things a lot clearer in packages so it's a good habit to have and in functions it'll help with matching up the end problem that caused your second error.
Lastly, if you want to return the user-defined type line_type you need to declare this _outside your function. Something like the following:
create or replace object t_line_type as object ( a varchar2(3000));
create or replace type line_type as varray(1000) of t_line_type;
Adding whitespace your function might look something like the following. This is my coding style and I'm definitely not suggesting that you should slavishly follow it but it helps to have some standardisation.
create or replace function f_vars ( PLine in varchar2
, PDelimiter in varchar2 default ','
) return line_type is
/* This function takes in a line and a delimiter, splits
it on the delimiter and returns it in a varray.
*/
-- local variables are l_
l_line varchar2 (3000) := PLine;
l_pos number;
-- user defined types are t_
-- This is a varray.
t_line line_type;
begin
for i in 1 .. length(l_line) loop
-- Get the position of the first delimiter.
l_pos := instr(l_line, PDelimiter, 1, 1);
-- Exit when we have run out of delimiters.
if l_pos = 0 then
t_line_var(i) := l_line;
exit;
end if;
-- Fill in the varray and take the part of a string
-- between our previous delimiter and the next.
t_line_var(i) := substr(l_line, 1, l_pos - 1);
l_line := substr(l_line, l_pos + 1, length(l_line));
end loop;
return t_line;
end f_vars;
/