Run stored procedure included sql script using robot framework - mysql

I want to run sql script which include db and table creations and stored procedure creations.
but when I try to run sql script using execute sql script keyword in database library I get an error like below
ProgrammingError: (1064, "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 'DELIMITER $$\n CREATE OR
REPLACE PROCEDURE `proc_GetCustomerDetails`(\n I...' at line 2")
before procedure I have delimiter like this,
DELIMITER $$
CREATE OR REPLACE PROCEDURE `proc_GetCustomerDetails`(
IN CustomerNbr LONGTEXT,
IN Lang VARCHAR(5)
)
DETERMINISTIC
BEGIN
IF Lang IS NULL THEN SET lang = "fin";
END IF;
SELECT * from dbname.customer;
END;$$
DELIMITER ;
If I comment stored procedure part, sql file is running without errors with rest of the table creation statements.
I googled this and couldn't find any related issue. I saw we have call stored procedure keyword. but I want to keep table creations and stored procedures in same sql file and need to run. I use MariaDB for this task.
Libraries used,
pymysql
robotframework-databaselibrary
If I run sql file using HeidiSQL it is running without any errors with procedures and delimiters. That mean there are no sql errors.
Can Someone tell me how to fix this?

DELIMITER is a statement supported only for the client, it is not supported by the server; thus the error. The solution - drop it.
Here's a question with very good answers what is it and why it's needed.
In short - when you work with a client you need a way to instruct it "this is not a statement you should execute immediately, this is still just a line in the SP you'll be sending to the server" - so you tell (to the client) "the DELIMITER b/n statements is temporarily $$". The server doesn't need/care about that - it knows everything between CREATE PROCEDURE, BEGIN, END are connected statements, a block.
When you connect to the DB through API (pymysql) vs an interactive client (shell, heidisql, etc) - you're sending the SP as a block, there's no way its statements will be ran one by one, thus the DELIMITER is not needed, not a supported command by the server, and generates an error. Drop it.

Related

Creating stored procedure on MySQL

I'm trying to run the query below, that is stored in a .sql file, then read with ioutils.ReadFile and executed on initialization
CREATE TABLE IF NOT EXISTS districts
(
GeoCode integer PRIMARY KEY,
name varchar(32)
);
drop procedure if exists insert_district;
DELIMITER $$
CREATE PROCEDURE insert_district(in pgeocode int, in pname varchar(32))
BEGIN
INSERT INTO districts(geocode, name) VALUES (pgeocode, pname);
SELECT * FROM districts where geocode = pgeocode;
END$$
DELIMITER ;
I am using the database/sql package and run the query with Exec
Error 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 'drop procedure if exists insert_district;
DELIMITER $$
CREATE PROCEDURE insert' at line 7
To the best of my knowledge my syntax is correct, and I tested it so I cannot figure out why the same exact query cannot be run properly from the program.
The Go MySQL client does not support multiple SQL statements by default. You can't just feed it a text file with ; separated statements.
See Does a Go Mysql driver exist that supports multiple statements within a single string? for details — there's an option you can use to allow multi-statements.
But that still won't support statements like DELIMITER which are not recognized by the MySQL Server. That's a mysql client command.
You have two alternatives:
Parse the .sql file to find statement terminators and run the statements one at a time.
Execute the mysql client in a sub-process using the .sql file as input.

"the following query has failed" error executing a stored routine

I have created the following stored procedure in phpMyAdmin Version: 4.1.14.3 running on a ZYXEL NS325 NAS. It appears in the routines list for the database.
DROP PROCEDURE `getDuplicateApplicationCyclePersons`;
CREATE DEFINER=`root`#`localhost`
PROCEDURE `getDuplicateApplicationCyclePersons`()
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
SELECT * FROM ApplicationCycle
However, when I execute the routine from the routines list, I get "the following query failed" but with no error details i.e. "MySql said:"
The query runs in the SQL window. I have tried several simple procedures on this new database all with the same result. Any ideas why they wont run?

error 1064 in trigger mysql

This is my first time using mysql and I am tying to learn how to use trigger.
Im using navicat, i go to table design and then go triggers tab. I create a trigger named testing and in definition I typed:
delimiter |
CREATE TRIGGER lpr.mytesting AFTER INSERT ON lpr.lpr_opt_upload
FOR EACH ROW BEGIN
set new.lpr_text := reverse(new.lpr_text);
END;
| delimiter ;
All im tying to do is whenever something new is inserted, I reverse the text in lpr_text field. However, i get "1064 - you have an error in your SQL; check the manual that corresponds o your MySql server version for the right syntax to yse 'ON lpr_opt_upload' FOR EACH ROW create trigger testing before insert on lpr_op' at line 1." I dont understand what Im doing wrong, I am just copying an trigger example.
//-------------------------------------------------------------------------//
I figured out the problem. I am using navicat and in navicat trigger tab, you only type the body into the definition, Not the header (ex: CREATE TRIGGER lpr.mytesting AFTER INSERT ON lpr.lpr_opt_upload). There are check box next to the name of your trigger and you use those instead of writing your own header.
The DELIMITER command is a client command, which not all clients support (it doesn't get sent to the server at all, it just instructs the client how to tell statements apart in order that they get sent to the server correctly). References to it in the MySQL manual assume that you are using the 'official' MySQL clients, such as the mysql command-line tool or MySQL Workbench (both of which support the DELIMITER command).
I don't know how to change the statement delimiter in Navicat, but that is the root of your problem.

How to insert the SQL Error message into table

I want to store the error messages which occurs while executing the stored procedure in another error table.
Here is the my sample procedure having some error statements.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`$$
CREATE PROCEDURE `test`()
BEGIN
SELECT * FROM emp;
END $$
DELIMITER $$;
When i call the above procedure it gives me error.I wnat to store this error code & message in another table as "error".
Any pointers are appreciated.
Thanks in Advance.
The standard way to handle this in MySQL is to declare a HANDLER to handle the error condition the way you want. This allows you to insert an error message into another table if you want, and then to either CONTINUE or EXIT the running procedure as required.
Here's the documentation:
http://dev.mysql.com/doc/refman/5.0/en/declare-handler.html
Unfortunately you will not be able to access the SQLSTATE of the statement that caused the error, so this approach is somewhat limited.
Here's another relevant question on SO with much more detail:
MySQL Stored Procedure Error Handling
SQL ERROR means there is something wrong with your SQL Query. It may (or may not) depend on your table. If the error is TABLE SPECIFIC or QUERY SYNTAX ERROR, that is, if there is something wrong with a specific table ONLY, or with a query, then definitely you can insert your errors into a table. But, if there is something with your CONNECTION or something else, then you cannot insert errors into any table.
ALWAYS try to log your errors in a html file or txt (text) file, so that you can smoothly access it. Also there is a less chance for failure.

Trouble creating stored procedure

I'm messing around with stored procedures for the first time, but can't even create a simple select! I'm using phpMyAdmin and this is my SQL:
DELIMITER //
CREATE PROCEDURE test_select()
BEGIN
SELECT * FROM products LIMIT 10;
END //
DELIMITER ;
After submitting that, my localhost does some thinking for a loooong time and eventually loads a page with no content called /phpmyadmin/import.php. After reloading phpMyAdmin and trying to invoke the procedure:
CALL test_select();
I get a "PROCEDURE doesn't exist" error. Any ideas?
Try to use the delimiter field of phpMyAdmin, as shown in the screenshot below:
Simply put the following in the query window:
CREATE PROCEDURE test_select()
BEGIN
SELECT * FROM products LIMIT 10;
END
In addition note that there is bug in some older versions of phpMyAdmin, which can cause an error when you call stored procedures that contain SELECT statements from phpMyAdmin.
You may want to check out the following posts for further reading:
MySQL Stored Procedures not working with SELECT (basic question)
How do I write an SP in phpMyAdmin (MySQL)?
This bug effects only phpMyAdmin, and you would still be able to call the stored procedure from anywhere else.