sql 1064 error when creating a stored procedure - mysql

i am attempting to multiple values from 2 tables and display them in a table join. ive been trying to get this to work using a stored procedure. my query works fine as when i tested it removing the argument and inserting a value i got results. i have never used stored procedures before and after reformatting the query and checking spellings and such i cant get it to work.
mysql> DELIMITER $$
mysql>
mysql> DROP PROCEDURE IF EXISTS `parts`.`proc_weight` $$
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE PROCEDURE `parts`.`proc_weight` (IN inputSupplier varchar(45))
->
-> BEGIN
-> SELECT parts.p_nr, project_parts_required.required_qty * parts.weight AS total_weight
-> FROM parts
-> INNER JOIN project_parts_required ON parts.p_nr = project_parts_required.part_nr
-> WHERE project_parts_required.s_nr = inputSupplier
-> END $$
ERROR 1064 (42000): 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 'END'
at line 8
mysql>
mysql> DELIMITER ;

Related

Stored Procedure MySQL Syntax error in Adminer

I'm very new to stored procedures in MySQL in general. I am trying to create one in Adminer and I keep getting a syntax error message:
Syntax error near '$$ CREATE PROCEDURE test() BEGIN SELECT * from lead; END$$ DELIMITER' at line 2
I'm trying to create a procedure named test to select all the records from a table. The code I put into Adminer is as follows:
DELIMITER $$
CREATE PROCEDURE test()
BEGIN
SELECT * from lead;
END$$
DELIMITER ;
However, if use the MySQL CLI, and enter the exact same code line for line, it works
mysql> DELIMITER $$
mysql> CREATE PROCEDURE test()
-> BEGIN
-> SELECT * from lead;
-> END$$
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
If I call the procedure test, I get all the records from the lead table.
I've logged into both Adminer and the CLI with the same credentials so I'm fairly confident it's not a permissions thing. I know I am missing something; can any one assist with a point in the right direction?

Pretty printing not working for stored proc in MySQL

I am trying to write an simple stored procedure to just print out information from three different tables. The problem is that for some reason \G does not work from within a stored proc. I want the output to be readable so this is pretty important
This code works but doesnt display columns in an effective way
DELIMITER //
DROP PROCEDURE IF EXISTS `snapshot`//
CREATE PROCEDURE snapshot(IN employeeUsername varchar(255))
BEGIN
SELECT * FROM employee where username = employeeUsername;
END //
DELIMITER ;
This code throws an exception
DELIMITER //
DROP PROCEDURE IF EXISTS `snapshot`//
CREATE PROCEDURE snapshot(IN employeeUsername varchar(255))
BEGIN
SELECT * FROM employee where username = employeeUsername \G;
END //
DELIMITER ;
ERROR 1064 (42000): 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 3
Any help would be greatly appreciated
\G is an command in MySQL Client and subsequently can not be called from a sql stored proc

INSERT Stored procedure not working for mySql

I'm trying to create DELETE stored procedure for a mysql table. The query runs and shows that the query was successful but when I try to view the stored procedure in 'routines', it doesn't show the DELETE stored procedure. This is my code for creating the stored procedure
DELIMITER //
CREATE PROCEDURE sp_AccessLevel_Delete(IN id INT)
BEGIN
DELETE AccessLevel
WHERE AccessLevelID = id;
END
//
DELIMITER ;
What can I do to fix this?
#siyual: putting this here since it won't fit into a comment:
[test]> delete foo where bar=2;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where bar=2' at line 1
[test]> delete foo;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
[test]> delete from foo;
Query OK, 1 row affected (0.00 sec)

MySql Stored Procedure mangles str_to_date() function

MySql Stored Procedure mangles str_to_date() function.
When the following MySQL query is run in the terminal mode it works fine.
UPDATE `tt002grawdata` SET `bizdate` = str_to_date(`BusinessDate`,'%m/%d/%y');
Query OK, 75 rows affected, 2028 warnings (0.01 sec)
Rows matched: 2028 Changed: 75 Warnings: 0
However, when placed within a MySQL Stored Procedure the update query is mangled.
DELIMITER $$
CREATE PROCEDURE test_upd
BEGIN
UPDATE `tt002grawdata` SET `bizdate` = str_to_date(`BusinessDate`,'%m/%d/%y');
End $$
DELIMITER ;
Please Note str_to_date() function has changed thus gives an error.
ERROR 1064 (42000): 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'BEGIN
UPDATE `tt002grawdata` SET `bizdate` = str_to_date(`BusinessDate`,'%m/%d/'
at line 2
Is there a work-a-round for this problem ?
Thanks.
Edward.
You need a () along with your stored procedure name. Your procedure body should look like below. Refer MySQL Documentation On Create procedure for more information.
DELIMITER $$
CREATE PROCEDURE test_upd()
BEGIN
UPDATE `tt002grawdata` SET `bizdate` = str_to_date(`BusinessDate`,'%m/%d/%y');
End $$
DELIMITER ;

mySQL Trigger returns error

I am trying to set up a trigger on a table to copy the contents of a row into another table.
I have the following:
CREATE TRIGGER story_deleted BEFORE DELETE ON stories
FOR EACH ROW BEGIN
INSERT INTO stories_backup SET story_id = OLD.story_id;
END;
This returns the following error though:
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 '' at line 3
I can't work out where I'm going wrong with this. Any ideas?
Try changing the delimiter
DELIMITER $$
CREATE TRIGGER story_deleted BEFORE DELETE ON stories
FOR EACH ROW
BEGIN
INSERT INTO stories_backup SET story_id = OLD.story_id;
END $$
DELIMITER ;
and as far as your privileges go, run this query
SHOW GRANTS;
If SUPER is not there, you could
request your DBA to add that privilege for you
have your DBA create the trigger for you