I have a mysql program that has out parameters and some print statements for debugging. I'm calling this procedure from a Unix shell script and the idea is to assign the out parameter to a unix variable which I use to check the condition later. But the variable is getting the print statement to be assigned as well. I want to get rid of the print statement and just assign the output parameters (integers) to variables.
Unix script:
output_var=`mysql -sN test_db -e "call delete_me(@outs);select @outs"` echo $output_var
Also gives print statements
proc started proc ended 1043
When I run the code in a SQL client like DBeaver..I only get the output parameters..
call delete_me(@out);select @out
@out ---- 1043
I don't want to disable print/debug statements as they are helpful for my logs. But I don't want them to appear in mysql out variables and ultimately my unix variables as well.
This is mysql program
CREATE PROCEDURE `delete_me`(out success int) BEGIN DECLARE var1 INT; DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN SHOW ERRORS; ROLLBACK; SET success=1; END; START TRANSACTION; select max(run_id) into var1 from job_run; select 'proc started'; select 'proc ended'; SET success:= var1; END;
Since the "debug" statement outputs to stdout, and
select @out;
also outputs to stdout, the MySQL client cannot distinguish between them. The process does not have output redirection functionality.So you have to think creatively.
One idea is to make sure your debug statements have some common pattern so that you can filter them out. For example, use the word "#DEBUG" consistently.
Then filter it when calling it from Bash:
Another idea is to change the code so that the debug statements are only output conditionally.
So if you want debug output, you can call it like this:
If you do not require debug output, do not set
@debug=1
.The most important thing is that MySQL stored procedures are really inconvenient in many situations. No real debugger, no packages, no compiler, no standard library, no atomic deployment, etc. I've been using MySQL for many years, but I've seen very few good uses for the program.
Assumption:
select @out
are shown on separate lines (as in the OP's original question)We'll start with a copy of the OP's current method of capturing all output to variables:
To append everything but the last line to the log file:
To capture the last line into a new variable: