I'm using MySQL version 5.1.60 and I have a script like the following:
START TRANSACTION;
BEGIN;
USE myDB;
DELETE FROM mytbl WHERE .......
DELETE FROM mytbl2 WHERE ......
....
INSERT INTO mytblN VALUE (.......
UPDATE mytbl SET .....
....
COMMIT;
Now, if any statement fails for any reason the execution of the script stops, I get the error message and I have to issue manually a ROLLBACK; and fix the relevant statement.
This script contains from hundreds to thousands statements and if many fails it is hard to fix each statement one by one.
I'm looking for a way to get all the errors in one shot and to have the transaction to rollback automatically.
Somewhere I found that if I replace the final
COMMIT;
with the following
SELECT IF(##error_count=0,'COMMIT;','ROLLBACK;') INTO #res;
PREPARE stmt FROM #res;
EXECUTE stmt;
and combine it with a
--force
parameter executing the script from command line I would get what I wanted.
Unfortunately I found that the system variable ##error_count 'reset' after each statement, and also the MySQL documentation states it:
error_count
The number of errors that resulted from the last statement that
generated messages. This variable is read only. See Section 14.7.5.17,
“SHOW ERRORS Syntax”.
Is there a way, without using a Stored Procedure, to obtain what I want?
So when I call the script from command line like:
>mysql -u root -p --force < myscript.sql >log.txt 2>&1
I have all the error logged in the log.txt file and the transaction rolled back?
many thanks in advance.
Related
I have a data cleanup script that I execute in Mysql Workbench in Windows. At the beginning of the script I have:
select ##hostname;
-- WARNING: it HAS to be `srv-datatest`
(+ the rest of the script)
I want the script to stop at that line if the condition (##hostname = 'srv-datatest') is not met.
Things that I've tried:
How to throw an exception:
DECLARE invalid_database CONDITION FOR 1051;
SIGNAL invalid_database;
-- DECLARE is not accepted by my workbench, and it lacks the "if" part anyway
How to do an IF on a select:
SELECT IF(##hostname='srv-datatest','yes','no');
-- It lacks the "stop here" part
create a stored procedure that will perform your process and test.
your script in mysql workbench should only call your stored procedure
It is possible to run it without stored procedures, using this ingenious solution:
SET #hostname := (SELECT ##hostname);
SET #condition = (#hostname='srv-datatest');
SET SESSION sql_mode = if(#condition, ##SESSION.sql_mode, 'Script stopped. Check condition.');
In this code:
If #condition=1, the script will run smoothly.
If #condition=0 it will throw an exception, showing the message error.
I am working on a script to reload a table using SQLCMD on Linux connecting to an Azure SQL Data Warehouse database.
After the INSERT statement completes, the next statement fails (but does not end the sqlcmd execution) with the "warning"
insert into
schema.table_temp
(
...list of columns
)
select
...list of columns
from
schema.table
;
GO (COMMENT--> in script, but not echoed in log.)
(0 rows affected)
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES t WHERE TABLE_TYPE = 'BASE TABLE' and TABLE_SCHEMA = 'schema' AND TABLE_NAME = 'table_nox' )
DROP TABLE schema.table_nox
;
GO (COMMENT--> in script, but not echoed in log.)
SqlState 24000, Invalid cursor state
The script continues to run with each subsequent batch getting the same
SqlState 24000, Invalid cursor state "warning"
If I comment out the INSERT statement, the script runs without warning as expected. I speculate the INSERT statement is not closing a cursor and then subsequent commands get the warning which should really be considered an eror and end the execution. (I have the -b flag on in the sqlcmd invocation.)
Is my only solution to break the script into multiple parts?
this seems to be an issue with the ODBC Driver that is used by sqlcmd under the hood. Can you confirm if you see this issue with Azure SQL DB? It would also be good to see your odbc.ini and odbcinst.ini files. Can you share those out?
In the meanwhile, I would like to recommend a couple of very cool Node.js command line tools that are more suited for Linux and Mac environments:
SQL-CLI
Cheetah
Thanks,
Meet
I faced similar issue and adding GO between two statements helped me.
We had the same issue. When performing INSERT via a SELECT it resulted in a (0 rows affected) response and a invalid cursor state error. Adding SET NOCOUNT ON at the start of the script worked for us. As #meet-bhagdev has already pointed out it looks like a driver issue.
I'm dipping my toe into using stored functions with MySQL and am having trouble.
Having created a function and tested it, I don't seem to be able to allow other users to execute it. From the documentation, it seems that I need to grant EXECUTE access to the other users, but that doesn't appear to be sufficient.
I've put together a couple of scripts that I believe demonstrate the problem:
# This script creates two databases with a stored function in each.
#
# On one database, tester in granted all privileges.
# On the other, tester only gets a few.
#
# We want to find the minimum privileges required to allow tester to execute the
# stored function.
#
# This script must be run by an administrative user, i.e. root
CREATE DATABASE test1;
DELIMITER $$
CREATE FUNCTION test1.foo () RETURNS VARCHAR(255) DETERMINISTIC
BEGIN
RETURN ('garp');
END$$
DELIMITER ;
GRANT ALL PRIVILEGES ON test1.* TO 'tester'#'localhost';
#
CREATE DATABASE test2;
DELIMITER $$
CREATE FUNCTION test2.foo () RETURNS VARCHAR(255) DETERMINISTIC
BEGIN
RETURN ('garp');
END$$
DELIMITER ;
GRANT EXECUTE ON PROCEDURE test2.foo TO 'tester'#'localhost';
and
# This script tests whether tester can access the stored functions
#
# It should be executed by tester
SELECT 'test1.foo(): ', test1.foo ();
SELECT 'test2.foo(): ', test2.foo ();
When I run execute the second script, I get an error:
$ mysql --user=tester --password=tester --skip-column-names < testScript2.sql
test1.foo(): garp
ERROR 1370 (42000) at line 6: execute command denied to user 'tester'#'localhost' for routine 'test2.foo'
I have no doubt that I'm missing something obvious, but I can't see what that is. I imagine that I've got something wrong in the GRANT EXECUTE... statement in the first script, and am deeply suspicious of my use of single quotes, but I recall trying most of the combinations of placement and inclusion of single quotes without success.
I'd be really grateful to anyone who can point out my error.
For reference, I'm running Server version: 5.1.67-0ubuntu0.10.04.1 (Ubuntu) (on Ubuntu!).
Thanks
test2.foo is a function not a procedure.
Try:
GRANT EXECUTE ON FUNCTION test2.foo TO 'tester'#'localhost';
(I was able to reproduce the problem locally and confirm that this change works.)
i have written a c-shell script to connect to a database. This already works just fine and i now want to invoke an sql script to read and print ALL the values in a cetrain table. As of now this is how my script looks
#!/bin/csh
set MYSQL=${MYSQL_HOME}/mysql
${MYSQL} ${CLEDBUSER}
where CLEDBUSER is set as an environment variable like so - CLEADBUSER=-uusername -ppassword -Ddatabasename
i am able to run the script and connect to the database. When i runt he script it gives me the msql pront awaiting the next command. So i added to the script a variable that contains the (SELECT) statement to query the database. Now my script looks like this
#!/bin/csh
set MYSQL=${MYSQL_HOME}/mysql
set SELECTER="SELECT * FROM TB_EARTH_UI;"
${MYSQL} ${CLEDBUSER} ${SELECTER}
the problem is it doesnt return me all the rows and columsn but it returns me a listing of avaiable commands in mysql promt and default options and also vairables and boolean options. Why is my SELECT statement not getting read?
MySQL client (mysql) expects SQL instructions on its standard input (e.g. your keyboard, when invoking from the shell).
You could do: [edit: please ignore, this one is off-topic]
${MYSQL} ${CLEDBUSER} < text_file_of_sql_statements.sql
or
${MYSQL} ${CLEDBUSER} << EOF
${SELECTER}
# you can add other litteral SQL statements here, or more variables containing SQL statements
EOF
or
${MYSQL} ${CLEDBUSER} --execute="${SELECTER}"
[edit]
I totally misunderstood the OP's question. I didn't get it that he was trying to execute SQL statements from a variable. I have edited the above options (thank you outis). Here is another variation:
echo ${SELECTER} | ${MYSQL} ${CLEDBUSER}
Also, the --skip-column-names option could make it easier for you to parse the output.
I'm having a rather strange problem with MySQL. Trying to create a procedure to update some fields in the database (the code is below).
The problem is with the line that is currently commented. It seems that if no SELECT statements get executed during the procedure MySQL query browser will return an error code of "-1, error executing SQL query".
I tried the same thing in HeidiSQL and the error was "cannot return result set". So I suppose the question is do I always have to select something in the procedure, or is there some other thing I missed.
The query works fine when the comment is removed.
DELIMITER /
DROP PROCEDURE IF EXISTS updateFavourites /
CREATE PROCEDURE updateFavourites(quota INT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE artist_id,releases INT;
DECLARE c_artist Cursor FOR
SELECT Artist.id_number,COUNT(Artist.id_number) FROM Artist
JOIN CD ON CD.is_fronted_by = Artist.id_number
GROUP BY Artist.id_number;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'
SET done=1;
IF quota > 0 THEN
OPEN c_artist;
REPEAT
FETCH c_artist INTO artist_id,releases;
IF NOT done THEN
IF releases >= quota THEN
UPDATE CD SET CD.rating='favourite' WHERE CD.is_fronted_by = artist_id;
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE c_artist;
-- SELECT 'Great success';
ELSE
SELECT CONCAT('\'quota\' must be greater than 0.',' Got (',quota,')');
END IF;
END /
DELIMITER ;
Here's the sql to create the tables and some data:
DROP TABLE IF EXISTS CD;
DROP TABLE IF EXISTS Artist;
CREATE TABLE Artist (
id_number INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
);
CREATE TABLE CD (
catalog_no INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
is_fronted_by INT UNSIGNED,
rating ENUM ('favourite','top draw','good','so-so','poor','rubbish'),
CONSTRAINT fk_CD_Artist FOREIGN KEY (is_fronted_by) REFERENCES Artist(id_number) ON UPDATE CASCADE
);
INSERT INTO Artist VALUES(11,'Artist 1');
INSERT INTO Artist VALUES(10,'Artist 2');
INSERT INTO CD VALUES (7,11, 'top draw');
INSERT INTO CD VALUES (650,11,'good');
INSERT INTO CD VALUES (651,11,'good');
INSERT INTO CD VALUES (11,10,'favourite');
Query Browser is not for running scripts, just single query.
I tried your code by moving cursor into each query (except DELIMITER) and pressing Ctrl+Enter.
It created that stored procedure without problem. (just refresh schema on the left).
If you wish creating procedure, use menu "Script"->"Create stored procedure/function".
But better forget about QueryBrowser it is not supported at all (and actunally not useful).
If you have decent hardware and plenty resources, try Workbench 5.2 otherwise use SQLyog
Googling around, there are several reports of the same error, but little information to solve the problem. There's even a bug logged at mysql.com but it appears to have been abandoned without being resolved.
There's another StackOverflow question on the same error, but it's also unresolved.
All it means is that there is no result set from the query. Looking at the source code, it appears that sometimes an error status of MYX_SQL_ERROR is set when the query has no result set. Perhaps this is not an appropriate consequence?
I notice that when I use the mysql command-line client, it yields no error for calling a proc that returns no result set.
update: I tried to revive that MySQL bug report, and provide a good test case for them. They changed the bug from "no feedback" to "verified" -- so at least they acknowledge it's a bug in Query Browser:
[11 Dec 9:18] Sveta Smirnova
Bill,
thank you for the feedback. Verified
as described.
Although most likely this only be
fixed when MySQL Query Browser
functionality is part of MySQL
workbench.
I guess the workaround is to ignore the -1 error, or to test your stored procedures in the command-line mysql client, where the error does not occur.
The comment supposes the issue will disappear as the Query Browser functionality becomes part of MySQL Workbench. This is supposed to happen in MySQL Workbench 5.2. I'll download this beta and give it a try.
MySQL Workbench 5.2 is in Beta, but I would assume MySQL engineering can't predict when the Beta will become GA. Those kinds of predictions are hard enough under standard conditions, but there's a lot of extra uncertainty of MySQL's fate due to the unresolved Oracle acquisition.
update: Okay, I have tried MySQL Workbench 5.2.10 beta. I executed a stored procedure like this:
CREATE PROCEDURE FooProc(doquery SMALLINT)
BEGIN
IF doquery THEN
SELECT * FROM Foo;
END IF;
END
When I CALL FooProc(0) the response is no result set, and the status is simply "OK".
When I CALL FooProc(1) the response is the result of SELECT * FROM Foo as expected.
However, there's another bug related to calling procedures. Procedures may have multiple result sets, so it's hard to know when to close the statement when you execute a CALL query. The consequence is that MySQL Workbench 5.2 doesn't close the statement, and if you try to do another query (either CALL or SELECT) it gives you an error:
Commands out of sync; you can't run this command now.
MySQL doesn't support multiple concurrent open queries. So the last one must be closed before you can start a new one. But it isn't closing the CALL query. This bug is also logged at the MySQL site.
The bug about commands out of sync has been resolved. They say it's fixed in MySQL Workbench 5.2.11.
Try putting BEGIN and END blocks around the multiple statements in the IF block as such:
IF quota > 0 THEN
BEGIN
OPEN c_artist;
REPEAT
FETCH c_artist INTO artist_id,releases;
IF NOT done THEN
IF releases >= quota THEN
UPDATE CD SET CD.rating='favourite' WHERE CD.is_fronted_by = artist_id;
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE c_artist;
END;
ELSE
SELECT CONCAT('\'quota\' must be greater than 0.',' Got (',quota,')');
END IF;