can't get MySQL query to work - mysql

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 ;

Related

Created a procedure for merging two tables branch_mstr and new_branches (having only two fields and same primary key)

delimiter $$
create procedure mergeData()
begin
declare counter, rowws int default 0;
set rowws = (select count(*) from new_branches);
begin
while counter < rowws do
if new_branches.branch_no != branch_mstr.branch_no then
insert into branch_mstr value(new_branches.branch_no, new_branches.name);
end while;
end;
end$$
delimiter ;
Why these crosses are showing, I am using MySQL and new to the PL/SQL. Asking for the help

Looking for a solution for this IF statement in 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

Nested if-statement in MySQL

I am making a procedure that inserts a place ("Sted") and I would like to check if the inputs are NULL. However, whenever I try to add an if-statement at the start to surround my code (marked CRASH below), it gives me an error saying my syntax is not right at "DECLARE varStedskodeID INT;" which is the part after the IF-statement I'm trying to add.
To my eyes the syntax of my if-statement is the same inside the code, but only my soon-to-be-NULL-check if-statement crashes even with just a simple IF(TRUE) THEN.
Can anyone give me a hint of what causes this one if to crash?
DROP PROCEDURE IF EXISTS InsertSted;
DELIMITER $$
CREATE PROCEDURE InsertSted(
IN inputStedsnavn VARCHAR(255),
IN inputStedstype VARCHAR(255),
IN inputKommunenavn VARCHAR(255))
BEGIN
IF(TRUE) THEN <<------ CRASH
DECLARE varStedskodeID INT;
DECLARE varKommunenr INT;
IF(SELECT COUNT(StedkodeID) FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1) = 0 THEN
INSERT INTO stedstype VALUES(DEFAULT, inputStedstype);
END IF;
SET varStedskodeID = (SELECT StedkodeID FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1);
IF(SELECT COUNT(Kommunenr) FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1) = 1 THEN
SET varKommunenr = (SELECT Kommunenr FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1);
INSERT INTO sted VALUES(DEFAULT, inputStedsnavn, varStedskodeID, varKommunenr);
END IF;
END IF; <<------ CRASH
END$$
DELIMITER ;
DECLARE is permitted only inside a BEGIN ... END compound statement
and must be at its start, before any other statements.
http://dev.mysql.com/doc/refman/5.0/en/declare.html
MySQL follows strict rules for DECLARE. You have to DECLARE variables, tables, etc... at the beginning of Stored Procedure.
Change Stored Procedure like this
DECLARE varStedskodeID INT;
DECLARE varKommunenr INT;
IF(TRUE) THEN
IF(SELECT COUNT(StedkodeID) FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1) = 0 THEN
INSERT INTO stedstype VALUES(DEFAULT, inputStedstype);
END IF;
SET varStedskodeID = (SELECT StedkodeID FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1);
IF(SELECT COUNT(Kommunenr) FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1) = 1 THEN
SET varKommunenr = (SELECT Kommunenr FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1);
INSERT INTO sted VALUES(DEFAULT, inputStedsnavn, varStedskodeID, varKommunenr);
END IF;
END IF;

Syntax error in creating a trigger in MYSQL

I wrote a trigger which get trigered after update statement of table1. If there is no row in table1 based on the conditions, then update table2.
CREATE TRIGGER trigger1
AFTER UPDATE ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF new.column1 = 1 THEN
SELECT COUNT(1) INTO #cnt FROM my_database.table1
WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2
SET isDeleted = 1
WHERE userSeqID = new.userID;
END IF
It gives me error i nline number 4, I couldn't understand the problem
Every IF statement has to ended with END IF clause. Try this statement -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER UPDATE
ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF NEW.column1 = 1 THEN
SELECT COUNT(1) INTO cnt FROM my_database.table1 WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2 SET isDeleted = 1 WHERE userSeqID = NEW.userID;
END IF;
END IF;
END$$
DELIMITER ;
The error on line 4 is because you're not using a delimiter.
Try this:
DELIMITER //
create trigger trigger1 after update on my_database.table1
for each row
begin
declare cnt int;
...
end if;
END;//
DELIMITER ;
You'll also need an END IF on the line after the select statement, and a terminal END; to match your begin, as I have in my example.

MySQl storage procedures

I have a little problem. Looks like the procedure does not exist. Somehow it's dropped after the creation. I get different error each time i change something. I'm not really sure what's causing the error, maybe I'm not allowed to drop procedures and creating them in the same query.
I hope you guys can help me out.
drop procedure if exists refIntChk;
DELIMITER //
CREATE PROCEDURE refIntChk(IN district INT(11), OUT b INT(1))
BEGIN
DECLARE b INT(1);
IF district IN (select dist FROM t13)
THEN
SET b = 1;
ELSE
SET b = 0;
END IF;
END; //
DELIMITER ;
drop procedure gen if exists ;
DELIMITER //
CREATE PROCEDURE gen()
BEGIN
DECLARE rows INT(11) DEFAULT (SELECT COUNT(dist) FROM t13);
DECLARE district INT(11);
DECLARE custname VARCHAR(16);
DECLARE revenue FLOAT;
DECLARE x INT DEFAULT 10000;
DECLARE outvar INT(11);
WHILE x > 0
DO
SET district = FLOOR(RAND()*rows)+1;
CALL refIntChk(district, outvar);
IF outvar = 1
THEN
SET custname = substring(MD5(RAND()), -16);
SET revenue = (RAND() * 10);
INSERT INTO t14 VALUES(NULL, custname, district, revenue);
SET x = x - 1;
END IF;
END WHILE;
END;//
DELIMITER ;
CALL gen();
When you get errors, it's usually good to run each statement, one by one, and see which one is producing the error.
The second DROP procedure statement should be:
drop procedure if exists gen;