Looking for a solution for this IF statement in mySQL - mysql

I'm trying to create a IF statement with mySQL...
This is the if in Oracle SQL
CREATE OR REPLACE TRIGGER pruef
BEFORE INSERT ON belegung
FOR EACH ROW
DECLARE
counter_1 INTEGER;
beleg INTEGER;
max_platz INTEGER;
BEGIN
SELECT :new.kurs_id into beleg from dual;
SELECT COUNT (*) INTO counter_1 FROM belegung WHERE kurs_id = beleg;
SELECT max_personen INTO max_platz FROM KURS WHERE kurs_id = beleg;
IF counter_1 >= max_platz THEN
RAISE_APPLICATION_ERROR(-20101, 'Maximale Teilnehmerzahl überschritten');
END IF;
END;
/
And this is the mySQL Code
delimiter //
CREATE TRIGGER pruef_mysql
BEFORE INSERT ON BELEGUNG
FOR EACH ROW BEGIN
BEGIN
DECLARE 'counter_1' INT;
'beleg' INT;
'max_platz' INT;
SELECT new.kurs_id into 'beleg' FROM dual;
SELECT (*) INTO 'counter_1' FROM 'belegung' WHERE kurs_id=beleg;
SELECT 'max_personen' INTO 'max_platz' FROM 'KURS' WHERE kurs_id = beleg;
IF ('counter_1' >= 'max_platz', signal sqlstate -20101 set msgtext= 'Maximale Teilnehmerzahl überschritten');
END IF;
END//
DELIMITER ;
I don't know how I do this... Maybe searched on the wrong pages.

IF function and IF statement are two different things in MySQL
You want to use the later one
https://dev.mysql.com/doc/refman/8.0/en/if.html

Related

procedure, setting variable, repeat until, substring from one variable, mysql

I am looking for help with my sql procedure.
I would like to substring in loop each username, and alias from two input arguments and add it into table users. I have two procedures.
Sorry for Polish variable names and procedure names.
My first procedure is
CREATE DEFINER=`root`#`localhost` PROCEDURE `dodajZawodnika`(IN `dane` VARCHAR(50), IN `wyswietlanie` VARCHAR(50))
NO SQL
BEGIN
IF (SELECT 1 = 1 from zawodnicy where danezawodnika = dane AND dowyswietlenia=wyswietlanie) THEN
BEGIN
Select id from zawodnicy where danezawodnika=dane AND dowyswietlenia=wyswietlanie;
END;
ELSE
BEGIN
Insert INTO zawodnicy (danezawodnika, dowyswietlenia) VALUES(dane, wyswietlanie);
SELECT last_insert_id() AS ID;
END;
END IF;
END;
My second procedure is
CREATE DEFINER=`root`#`localhost` PROCEDURE `stworzSklad`(IN `dane` VARCHAR(1500), IN `pseudo` VARCHAR(1000))
NO SQL
BEGIN
DECLARE counter INTEGER;
DECLARE zawodnik VARCHAR(1500);
DECLARE pseudonim VARCHAR(1000);
DECLARE zawodnicy VARCHAR(1500);
DECLARE pseudonimy VARCHAR(1000);
DECLARE exit handler for sqlexception
BEGIN
-- ERROR
ROLLBACK;
END;
DECLARE exit handler for sqlwarning
BEGIN
-- WARNING
ROLLBACK;
END;
set counter = 0;
set #zawodnicy = dane;
set #pseudonimy = psuedo;
select #zawodnicy, #pseudonimy;
REPEAT
set zawodnik=(SELECT TRIM(SUBSTRING_INDEX(#zawodnicy, ',', 1)));
set pseudonim=(SELECT TRIM(SUBSTRING_INDEX(#pseudonimy, ',', 1)));
call dodajZawodnika(zawodnik, pseudonim);
SELECT RIGHT(#zawodnicy, TRIM(length(#zawodnicy) - length(SUBSTRING_INDEX(#zawodnicy, ',', 1)) - 1)) into #zawodnicy;
SELECT RIGHT(#pseudonimy, TRIM(length(#pseudonimy) - length(SUBSTRING_INDEX(#pseudonimy, ',', 1)) - 1)) into #pseudonimy;
select zawodnik as 'zawodnik', pseudonim as 'pseudonim', dane as 'dane', pseudo as 'pseudo', #zawodnicy as 'zawodnicy', #pseudonimy as 'pseudonimy';
set counter = counter + 1;
UNTIL (#zawodnicy = '' or counter = 30)
END REPEAT;
select counter;
END
I am fighting with it from about 2 hours.
I would like to use also transaction for this insert.
Please help me with this little problem.
How to make this in proper way?
Now, calling stworzSklad procedure make endless loop (ofc without counter in until condition) and any of user was not inserted in my table.
Cheers!

not allowed to return a resultset from a trigger mysql

delimiter $$
CREATE TRIGGER REDUCE_NOTE_COUNT
AFTER DELETE ON iv_notes
FOR EACH ROW BEGIN
DECLARE supplierid int(11);
DECLARE customerid int(11);
SELECT supplierid ,customerid FROM iv_documents WHERE id=OLD.note_documentid;
SET supplierid=supplierid;
SET customerid=customerid;
IF supplierid=OLD.note_companyid THEN
update iv_documents
set supplier_notes=supplier_notes-1
where id=OLD.note_documentid and supplier_notes>0;
END IF;
IF customerid=OLD.note_companyid THEN
update iv_documents set customer_notes=customer_notes-1
where id=OLD.note_documentid
and customer_notes>0 ;
END IF;
END$$
delimiter ;
You cannot execute SELECT statements from trigger. If you want to set variables, then use SELECT INTO statement, e.g. -
DECLARE supplierid_ INT(11);
DECLARE customerid_ INT(11);
SELECT
supplierid, customerid
INTO
supplierid_, customerid_
FROM
iv_documents
WHERE
id = OLD.note_documentid;
IF supplierid_ = OLD.note_companyid THEN
...
Also, rename variables, they have to differ from from field names.

can't get MySQL query to work

I am trying to create a stored procedure on a Library database for practice mainly.
But it is confusing me slightly. I am using this query to create the procedure, and it says that it is working, and yet when I then call the procedure, it is saying that the procedure does not exist. Note: I am using phpmyadmin on a web database not on a local host.
delimiter $$
create procedure BorrowBook(in theBookID, in theUserID)
begin
declare lim int;
declare num int;
declare loanNumber int;
declare copyNumber int;
set lim = (select Readers.Limit from Readers where Readers.id = theUserID);
set num = (select Count(*) from Loans where Loans.UserID = theUserID);
set loanNumber = (select Count(*) from Loans) + 1;
set copyNumber = (select NumberAvailable from Book where Book.id = theBookID);
if(copyNumber > 0)
if(num<lim)
then
--Add a Loan to the Loans Table
insert into Loans values(loanNumber, theBookId, theUserID, curDate(), 0);
commit;
update Book set Book.NumberAvailable = Book.NumberAvailable - 1 where Book.id = theBookID;
select 'Succesful Update';
else
rollback;
select 'Borrow limit reached';
end if;
else
rollback;
select 'No copies available';
end;
delimiter ;
You are missing the $$ after the last end:
delimiter $$
...
end; $$ <-- add $$ here
delimiter ;
Shouldn't it be
end; $$
delimiter ;
instead of
end;
delimiter ;

Apparently fetch returns NULL

I am learning stored procedures, cursors in mysql and I stumble on it:
delimiter //
CREATE PROCEDURE some_func()
BEGIN
DECLARE link_rewrite VARCHAR(255);
DECLARE link_rewrite_cursor CURSOR FOR SELECT link_rewrite FROM prod;
OPEN link_rewrite_cursor;
SET #count = 0;
WHILE #count < 10 DO
FETCH link_rewrite_cursor INTO link_rewrite;
SELECT link_rewrite;
set #count = #count + 1;
END WHILE;
CLOSE link_rewrite_cursor;
END//
delimiter ;
My question is: Why SELECT link_rewrite always returns NULL (in prod table there is 9000 rows). SELECT link_rewrite FROM prod returns a lot of rows(9000 rows).
You should avoid using the same name for multiple different things. Specifically, give the variable a different name than the column you are selecting. For example, if you rename the variable v_link_rewrite then it will probably work:
delimiter //
DROP PROCEDURE IF EXISTS some_func //
CREATE PROCEDURE some_func()
BEGIN
DECLARE v_link_rewrite VARCHAR(255);
DECLARE link_rewrite_cursor CURSOR FOR SELECT link_rewrite FROM prod;
OPEN link_rewrite_cursor;
SET #count = 0;
WHILE #count < 10 DO
FETCH link_rewrite_cursor INTO v_link_rewrite;
SELECT v_link_rewrite;
set #count = #count + 1;
END WHILE;
CLOSE link_rewrite_cursor;
END//
delimiter ;
If you just want to select the top 10 rows, do this:
select link_rewrite from prod limit 10
It's much quicker and you don't have to go with a cursor.

MySQL Syntax Error In Variable Declaration

I have the following MySQL query:
DELIMITER //
CREATE PROCEDURE InsertResult (IN winnerID INT, IN loserID INT)
BEGIN
INSERT INTO KomperResult (WinnerID, LoserID) VALUES (#winnerID, #loserID);
DECLARE winnerScore, loserScore INT;
SELECT Score INTO #winnerScore FROM KomperPerson WHERE ID = #winnerID;
SELECT Score INTO #loserScore FROM KomperPerson WHERE ID = #loserID;
IF (#loserScore >= #winnerScore) THEN UPDATE KomperPerson SET Score = #loserScore + 1 WHERE ID = #winnerID; END IF;
END//
I get an error on:
DECLARE winnerScore, loserScore INT;
What am I doing wrong?
DECLAREs need to go on the first line of your procedure.
From the docs:
DECLARE is permitted only inside a
BEGIN ... END compound statement and
must be at its start, before any other
statements.