sql if statement in store procedure - mysql

For some reason MYSQL will not let me create this procedure, can anybody see what the problem is?
It works if I remove the IF statement but to me the if statement looks ok.
delimiter $$
create procedure add_new_room(IN buildingID INT,
IN inName TEXT,
IN inWeeks INT,
IN inAnnual TEXT,
IN inWeekly TEXT)
BEGIN
DECLARE roomnameid INT;
DECLARE roomcount INT;
SELECT count(roomID) from tblRooms WHERE roomName = inName into roomcount;
if roomcount = 1 then
SELECT roomID from tblRooms
WHERE roomName = inName into roomnameid;
INSERT INTO tblAccommRoom
(roomWeeks,roomID,accommodationID,roomWeeklyRent, roomAnnualRent)
VALUES (inWeeks,roomnameid,buildingID,inWeekly, inAnnual);
else
INSERT INTO tblRooms (roomName) VALUES inName;
INSERT INTO tblAccommRoom
(roomWeeks, roomID, accommodationID, roomWeeklyRent, roomAnnualRent)
VALUES (inWeeks,LAST_INSERT_ID(),buildingID,inWeekly, inAnnual);
end if;
END

I would use a CASE WHEN statement instead of the IF statement, which allows the code to work a little more universally with MS SQL and MySQL.
CASE WHEN roomcount = 1 THEN...ELSE...END
I don't do a lot of work with MySQL, but I also typically see IF statements contained within parentheses and commas utilized rather than spelling out THEN and ELSE.
IF(condition,then statement,else statement)

Try changing the SELECT to this:
SELECT count(roomID) into roomcount from tblRooms WHERE roomName = inName;
then, you should be able to use 'roomcount' later on.

Related

select statement intermittently returns null

So I have the following code in a stored procedure:
set #Id = (select id from foo where Name = 'bar');
IF #Id is null THEN
#add missing record
END IF;
However, its seems that the database will only return a value intermittently. Even when I know my select statement will return a record (copied and pasted directly out of my stored proc). Has anyone else had this issue with MySQL?
about the only crazy thing my proc is doing:
DECLARE CONTINUE HANDLER FOR 1062, 1452
but neither of those should effect my query (I am sure there is one and only one record)
Thanks
You can use a stored procedure like the following:
CREATE PROCEDURE example_proc (pName VARCHAR(10))
BEGIN
IF NOT EXISTS(SELECT 1 FROM foo WHERE Name = pName) THEN
INSERT INTO foo (Name) VALUES (pName);
END IF;
END
demo on dbfiddle.uk
SO i don't know why this fixed the problem, but it did.
By setting the variable to null first before the assignment seems to have fixed the problem. I was have the problem intermittently, so maybe I'm just lucking out longer than I did last time.

mysql procedure with if condition

I'm in my first databases class and I'm trying to write a conditional block for a mysql procedure.
This is the procedure:
delimiter //
CREATE PROCEDURE add_ascent(IN cid INT, IN pid INT)
BEGIN
DECLARE count_ascents INT;
SET count_ascents = 0;
SELECT COUNT(`cid`) INTO count_ascents FROM ascents WHERE `cid`=cid AND `pid`=pid;
IF count_ascents < 1 THEN
INSERT INTO ascents (`cid`, `pid`) VALUES (cid, pid);
UPDATE climbers SET climbers.ascents = climbers.ascents + 1 WHERE climbers.id=cid;
UPDATE problems SET problems.ascents = problems.ascents + 1 WHERE problems.id=pid;
END IF;
END;
//
delimiter ;
The goal of the procedure is to only perform the insert and updates if the (cid, pid) pair is not in the the ascents database. After testing, the program doesn't seem to go into the if block at all.
FYI, you might want to consider using an UPSERT, instead of "select/if/insert". For example, mySQL offers INSERT ON DUPLICATE KEY UPDATE.
Here, I suggest:
giving your parameters a DIFFERENT name than the column name, for example iCid and iPid, then
Typing SELECT COUNT(cid) INTO count_ascents FROM ascents WHERE cid=iCid AND pid=iPid and checking the result.

MySQL stored procedure if statement not working

I have the following stored procedure in MySQL
CREATE DEFINER=`test_db`#`%` PROCEDURE `ADD_ATTENDANCE`(IN `programID` INT, IN `clientID` INT, IN `insDate` DATETIME, IN `updDate` DATETIME, IN `insUser` INT, IN `updUser` INT, IN `lessonDate` DATE, IN `lessonTime` TIME)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'Add attedance to my calendar'
BEGIN
DECLARE max_sub, availability INT;
DECLARE cursor_max_sub CURSOR FOR SELECT max_sub FROM app_lesson WHERE id = programID;
DECLARE cursor_availability CURSOR FOR SELECT count(*) FROM attendance WHERE program_id = programID AND lesson_date = lessonDate AND lesson_time = lessonTime;
OPEN cursor_max_sub;
OPEN cursor_availability;
read_loop: LOOP
FETCH cursor_max_sub INTO max_sub;
FETCH cursor_availability INTO availability;
IF (availability < max_sub) THEN
insert into attendance (program_id, client_id, ins_date, upd_date, ins_user, upd_user, lesson_date, lesson_time)
values (programID, clientID, insDate, updDate, insUser, updUser, lessonDate, lessonTime);
LEAVE read_loop;
ELSE
insert into attendance_hold (program_id, client_id, ins_date, upd_date, ins_user, upd_user, lesson_date, lesson_time)
values (programID, clientID, insDate, updDate, insUser, updUser, lessonDate, lessonTime);
END IF;
END LOOP;
CLOSE cursor_max_sub;
CLOSE cursor_availability;
END;
Even though the cursor_max_sub is equal to 6 and the cursor_availability is equal to 4 my procedure always executes the else insert statement. Can you please help me out?
Thanks!!!
OK that was tricky... For some reason when i change the max_sub variable into maxNumberOfSubscription everything worked perfectly... Is max_sub some kind of reserved key word for MySQL or there was a complication because my variable had the same name with the returned field of select statement?

stored procedure in SQL

I have a stored procedure that should check if an artist being added is in the Allowed Nationality table, but it doesn't work. The code inserts the artist whether they are in the Allowed Nationality table or not. Can anyone tell me what I have done wrong?
DELIMITER //
CREATE PROCEDURE `InsertNewArtistCheck`
(IN newLastName Char(25),
IN newFirstName Char(25),
IN newNationality Char(30),
IN newDateOfBirth Numeric(4),
IN newDateDeceased Numeric(4))
BEGIN
DECLARE varRowCount Int;
SELECT Nation INTO varRowCount
FROM ALLOWED_NATIONALITY
WHERE Nation = newNationality;
IF (varRowCount < 0)
THEN
ROLLBACK;
SELECT 'Nationality Not Allowed' AS ErrorMessage;
END IF;
INSERT INTO ARTIST (LastName, FirstName, Nationality,
DateOfBirth, DateDeceased)
VALUES (newLastName, newFirstName, newNationality,
newDateOfBirth, newDateDeceased);
SELECT 'New artist data added to database' AS InsertStatus;
END//
DELIMITER ;
Try the following changes:
DECLARE varRowCount Int;
SELECT count(*) INTO varRowCount
FROM ALLOWED_NATIONALITY
WHERE Nation = newNationality;
IF (varRowCount < 1)
THEN
ROLLBACK;
SELECT 'Nationality '+newnationality+' not Allowed' AS ErrorMessage;
RETURN
END IF;
You are trying to put a character value (NATION) into a numeric variable (varRowCount). What you really want is to determine the number of nations (hopefully 1) which match the new artist's nation. You also don't need the ROLLBACK statement, since the stored procedure has not done anything it needs to "undo"
WIll be closed, still....
See this line:
IF (varRowCount < 0)
Tell me under what conditions you think SQL Server will ever return a NEGATIVE number of rows?
Should be equals 0, not smaller than.

MySQL: IF / THEN statements in stored procedures

I'm writing a stored procedure that uses multiple IF / THEN statements that also need to execute multiple queries if they evaluate to true. Problem is, I can't seem to find any examples of the appropriate syntax. From the MySQL dev handbook, it seems like I could have multiple queries in the "statement_list," but so far I can't get it to work.
Here's what I'm trying to do:
SET agency =
COALESCE((SELECT org_agency_o_id
FROM orgs_agencies
WHERE org_agency_code = maj_agency_cat)
,(SELECT min(org_id)
FROM orgs
WHERE org_name LIKE CONCAT('U.S.',SUBSTRING(maj_agency_cat,5))))
IF agency IS NULL THEN
-- execute multiple queries
INSERT INTO orgs (org_name
,org_name_length
,org_type
,org_sub_types)
VALUES (CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5))
,LENGTH(CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5)))
,'org','Org,GovernmentEntity,Federal,Agency');
SET agency = LAST_INSERT_ID();
END IF;
The error:
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 'IF agency IS NULL THEN
INSERT INTO orgs (org_name,org_name_length,org_type,' at line 53
Any ideas? I know it has to be something simple, so I would greatly appreciate anybody's input.
You got a few issues as far as I can see:
As David pointed out, each and every statement needs to be terminated by a ;
If you do a SELECT, better make sure it can only select one value by doing a LIMIT 1; If you've got an aggregate function like min() then only one value can come out.
If you writing the procedure using the CREATE PROCEDURE ... syntax, don't forget to set DELIMITER $$ before the CREATE PROCEDURE ... END $$ body and a DELIMITER ; after.
If you have multiple statements inside your IF THEN ... END IF block, it's a good idea to put them inside a BEGIN ... END; block.
If you have a return value, like agency here, why not make it a FUNCTION name (arg1: INTEGER) RETURNS INTEGER instead of a PROCEDURE name (IN arg1 INTEGER, OUT agency INTEGER). The function is much more versatile.
DELIMITER $$
CREATE PROCEDURE name(arg1 INTEGER, arg2 INTEGER, ...)
BEGIN
SELECT SET agency =
COALESCE((SELECT org_agency_o_id
FROM orgs_agencies
WHERE org_agency_code = maj_agency_cat) LIMIT 1,
(SELECT min(org_id) FROM orgs
WHERE org_name LIKE CONCAT('U.S.',SUBSTRING(maj_agency_cat,5))));
IF agency IS NULL THEN BEGIN
-- execute multiple queries
INSERT INTO orgs (org_name
,org_name_length
,org_type
,org_sub_types)
VALUES (CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5))
,LENGTH(CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5)))
,'org','Org,GovernmentEntity,Federal,Agency');
SET agency = LAST_INSERT_ID();
END; END IF;
END $$
DELIMITER ;
No semicolon after your first SET statement.