Simple Mysql "insert" stored procedure won't work - mysql

I can't make this stored procedure load/save, which is very strange because the insert one line version of the procedure works with no problem. I am NOT trying to overload, I gave both procedures and all the variables different names The workbench is telling me that the problem lies with the parenthesis after "flavidtwo". Reversing the order of the inserts doesn't fix the problem, it just starts complaining about the end of the other line instead.
CREATE PROCEDURE `LinkTwoFlavors`(in selzidtwo INT, in flavidone INT, in flavidtwo INT)
BEGIN
INSERT INTO `readinga_seltzer`.`AscSeltzerFlavor` (`SeltzerID`,`FlavorID`) VALUES (selzidtwo, flavidtwo);
INSERT INTO `readinga_seltzer`.`AscSeltzerFlavor` (`SeltzerID`,`FlavorID`) VALUES (selzidtwo, flavidone);
END

Actually, it's complaining about the semi-colon, not the parenthesis. You need to set a different DELIMITER before your procedure (resetting it again afterwards) so that mysql knows to treat the whole procedure as a single statement otherwise it thinks it should end after your first INSERT statement. Your procedure should then look like this
DELIMITER //
CREATE PROCEDURE `LinkTwoFlavors`(in selzidtwo INT, in flavidone INT, in flavidtwo INT)
BEGIN
INSERT INTO `readinga_seltzer`.`AscSeltzerFlavor` (`SeltzerID`,`FlavorID`) VALUES (selzidtwo, flavidtwo);
INSERT INTO `readinga_seltzer`.`AscSeltzerFlavor` (`SeltzerID`,`FlavorID`) VALUES (selzidtwo, flavidone);
END //
DELIMITER ;

Related

MySQL Stored Procedure does not return output when passing input as a variable using SET syntax

Thank you for taking the time out to read my question.
I have a simple mySQL stored procedure as below that takes one input parameter and returns one output.
DELIMITER $$
CREATE PROCEDURE createOrder (in reqno varchar(20), out out_val varchar(20))
BEGIN
select custphone
into out_val
from request
where requestnum = reqno;
insert into test values (reqno, out_val );
END;
The stored procedure works when I execute it using the below steps, i.e. it returns the output if I pass the input parameter as a hard-coded string.
call createOrder('RQ000434', #out);
select #out;
However it returns NULL when I pass the first parameter as a variable, setting its value using the SET syntax:
SET #inv = 'RQ000455';
call createOrder(#inv, #out);
select #out;
I am new to mySQL and not able to see where I am going wrong. Any suggestion to resolve, is most welcome!
Thank you in advance! Best regards
Folks:
I was able to figure out the problem. After trying out different things, I finally was able to get the stored procedure to work. Here is my solution for those who are looking for an answer if they encounter a similar problem:
DELIMITER $$
CREATE PROCEDURE createOrder2 (in reqno varchar(20), out out_val varchar(20))
BEGIN
SET out_val = (select custphone
from request
where requestnum = reqno);
insert into test values (reqno, out_val );
END;
Now to call the Stored Procedure in Workbench,
SET #inv = 'RQ000434';
call createOrder2(#inv, #out);
select #out;
Thank you for those who took the time to read my question! Regards!

Yet another MySQL syntax error

This isn't something I usually have problems with but I don't understand that one now. I tried with backticks, with double quotes, without backticks,...
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 '' at line 7
CREATE PROCEDURE mapping(
p_object INT(10),
brand VARCHAR(255),
model VARCHAR(255))
BEGIN
INSERT INTO `object_brand_model_mapping`
SET `object`=p_object, `brandNameNormalized`=brand, `modelNameNormalized`=model;
END;
I also tried
INSERT INTO `object_brand_model_mapping`
(`object`, `brandNameNormalized`, `modelNameNormalized`)
VALUES
(p_object, brand, model);
which produces exactly the same error.
I don't know what's wrong with this. Do any special rules apply to INSERT in a procedure?
All of the columns do exist.
The parser is getting confused by having a common delimiter between defining the procedure and the procedure contents. To remedy this, define a different delimiter before creating the procedure:
DELIMITER $$
CREATE PROCEDURE mapping(
p_object INT(10),
brand VARCHAR(255),
model VARCHAR(255))
BEGIN
INSERT INTO `object_brand_model_mapping` (`object`, `brandNameNormalized`, `modelNameNormalized`)
VALUES (p_object, brand, model);
END$$
DELIMITER ;
From Alex Silverstein's article on the subject:
The next step is to change the default MySQL script parser’s delimiter from semicolon (;) to double-dollar sign ($$). The reason you do this is so that the semicolons after each statement in the body of the routine are not interpreted by the parser as meaning the end of the CREATE PROCEDURE statement. This is because the entire CREATE PROCEDURE block, from CREATE PROCEDURE to END is actually a single statement that must be executed by itself. Were it not for the delimiter change, the script would break, since there each statement inside BEGIN and END would execute individually. Note that you can use a variety of non-reserved characters to make your own custom delimiter.

Unexpected END_OF_INPUT in MySQL trigger

I have searched for all the possible online solutions but I can't figure out the error in this trigger.
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
BEGIN
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`;
END;
the first error appears at OLD.neat_link
syntax error, unexpected END_OF_INPUT, expecting ';'
and the second one at END;
syntax error, unexpected END
Any help would be appreciable, thanks.
That problem is due to interpreting individual statements. The CREATE TRIGGER statement is as such a single complete statement that must be sent as is to the server. Usually statement borders are recognized by the default delimiter (the semicolon). In case of stored programs however the semicolon is needed to separate inner statements. This would confuse the client as it cannot tell apart what is an inner statement of the stored program or a full statement as it must be sent as a whole to the server.
Hence the DELIMITER statement was introduced which only applies to clients (not the server, the server itself cannot parse this statement). It changes the default delimiter to one of your choice, leading so the client where to look for the statement's end. A typical case hence looks like this:
DELIMITER ;;
CREATE TRIGGER `ins_film` AFTER INSERT ON `film` FOR EACH ROW BEGIN
INSERT INTO film_text (film_id, title, description)
VALUES (new.film_id, new.title, new.description);
END;;
Their is only one statement in the body of the Trigger, so there is no need to use the BEGIN-END compound statement construct. Try this:
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`
another possible solution
DELIMITER $$
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
BEGIN
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`;
END$$
DELIMITER ;

MySQL Procedure Not Working

I'm working with procedures for the first time in MySQL, but for some reason I keep getting NULL. My test procedure is a simple one, it just adds.
delimiter $$
create procedure adds(in r double, out a double)
begin
set a = r + r;
end $$
delimiter ;
CALL adds(5, #a);
SELECT #a;
Not sure if I'm doing this right. For #a it just prints out NULL.
A procedure is linked to a database.
You have not specified one, and therefor it will probably be attached to a different database than the one you are expecting.
When you change databases, MySQL will not longer find your stored procedure because it only looks in the correct DB.
Remember to always specify your database when declaring a stored proc
create procedure mydatabase.adds(in r double, out a double)
^^^^^^^^^^^

Create stored procedure in mysqladmin?

I am trying to create stored procedure in mysql admin. I am hosting a website in POWWEB.com. For this purpose i am trying to create stored procedure in mysqladmin.
The Mysql version is 5.0.45. I am able to create a stored procedure with only 1 line of code.
CREATE PROCEDURE TEST(input INT)
INSERT INTO TEST(COL1) VALUES(input);
But this is of no use. I want to write a stored procedure with more commands. So i try doing like
CREATE PROCEDURE TEST(input INT)
BEGIN
INSERT INTO TEST(COL1) VALUES(input);
INSERT INTO TEST1(COL1) VALUES(input);
INSERT INTO TEST2(COL1) VALUES(input);
END
But this gives a syntax error. But the same code works fine in my local machine.
Please let me know if you have any idea of how to solve this. Any help or advice is greatly appreciated.
The default delimiter is ;, so it interprets the code as multiple queries, which obviously doesn't work. Try something like this:
delimiter //
CREATE PROCEDURE TEST(input INT)
BEGIN
INSERT INTO TEST(COL1) VALUES(input);
INSERT INTO TEST1(COL1) VALUES(input);
INSERT INTO TEST2(COL1) VALUES(input);
END//
Ignore PHPMyAdmin, it is as useless as a motorcycle-ashtray. Log in to your shell and use the mysql command line interface, this is the only supportable, correct way of scripting mysql.
The DELIMITER command is not a mysql server command, it is a mysql command-line client command. To use it you must use the proper mysql command line client.
It is difficult (although not impossible) to create stored procs otherwise.
In phpMyAdmin, in the SQL editor you can set the delimiter in a form field labeled 'delimiter'. It is by default. Set it to anything you like, do NOT type in delimiter // in your SQL but DO end the sql with your delimiter as END// where // is the delimiter of your choice.
the Best answer is just #Barry's comment:
Stored procedures with multiple queries work fine when putting a BEGIN .. END
block around it.
– Barry Staes
Plus, putting ; at end of each query.
example:
BEGIN
INSERT INTO passenger (fname,lname,tel,ID,sex)
VALUES (fname,lname,tel,ID, sex);
INSERT INTO passenger
select * from reservation where 1;
END
MySQL admin is outdated. Please use MySQL workbench for more functionalities and and rich GUI based operations.
https://www.mysql.com/products/workbench/