mysql using tee in stored procedure - mysql

I'm new to MySQL (or SQL in general)
I'm trying to get MySQL to write the timestamp into a file with stored procedure using TEE command (I don't think I can use "select into outfile" because I don't want to delete the file, I want to add a line to it...):
mysql> DELIMITER $$
mysql> CREATE PROCEDURE test_to_file()
-> begin
-> TEE /home/ubuntu/test.txt;
-> SELECT NOW();
-> end $$
However, I get 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 '/home/ubuntu/test.txt;
SELECT NOW();
end' at line 3
Thanks for the help

Disclaimer: I am not certain that whatever you are trying to do is necessarily a good idea... but there is a simple solution for this.
Create a table using the CSV Storage Engine.
You can then append to the file by simply inserting into this table.
Using tee is a client-side feature. It doesn't write to the server at all, unless you happen to be running the mysql client on the server machine... in which case, it's only writing to the server machine by coincidence.

Related

Invalid syntax in WHILE loop for MySQL 8.0.26

I'm running MySQL 8.0.26 inside a Linux container, using Docker.
I am attempting to run a stress test by infinitely selecting data from a table named employees in the employees database. To attempt this, I am embedding a SELECT statement inside of a WHILE loop, with a search condition that always returns true.
I am attempting to issue the following query against the MySQL engine, using the MySQL CLI tool.
WHILE 1=1 DO
SELECT * FROM employees.employees;
END WHILE;
When I run this query, I receive the error message as follows.
Error invoking remote method 'DB_EXECUTE_CANCELLABLE_QUERY': Error: 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 'WHILE 1=1 DO SELECT * FROM employees.employees; END WHILE' at line 1
According to the MySQL documentation, I am using the correct syntax. What is wrong with this SQL statement?
Docs: MySQL Comparison Operators
Docs: MySQL WHILE Statement
The WHILE statement is only valid inside MySQL stored procedures, functions.

White space changes the delimiter when running MySQL script from the shell/command prompt

I use MySQL innodb version 5.7.10 'MySQL Community Server (GPL)' on a Windows machine. The following script runs fine if I run it from MySQL Workbench:
DROP PROCEDURE IF EXISTS procedure1;
DELIMITER //
CREATE PROCEDURE procedure1(IN pageSize BIGINT)
BEGIN
SELECT * FROM table1 LIMIT pageSize;
END //
DELIMITER ; -- note that there is an extra tab char here
DROP PROCEDURE IF EXISTS procedure2;
DELIMITER //
CREATE PROCEDURE procedure2()
BEGIN
SELECT * FROM table2;
END //
DELIMITER ;
But if copy the script into schema.sql and run it from Windows command prompt:
mysql> c:\release\ver1\schema.sql
I get the following error:
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 'DELIMITER //
This error is because of the tab char, if I remove the tab, it will run fine.
I have also tried running it from Linux shell (MyQSL innodb version 5.7.25) and I get the same error:
mysql> source /home/ubuntu/release/ver1/schema.sql
Note: I have tried replacing the tab char (\t) with a couple of white space characters, and the white spaces are fine... it's only the tab char which changes the delimiter.
I am using MySQL Workbench for the Dev Environment, but for Test and Prod Environments, I am just using MySQL from Linux Shell... this error has caught me so many times, because the scripts which have passed the Dev Environment, fails in Test and every time I have to remember to go back and remove the tabs from the script.
Is there anyway to fix this issue or configure MySQL to ignore tab char?
There are programs like GrepWin that can easily replace characters in files. However, if you want something automated, I would recommend you setup a webhook and have a command to replace all tabs like below execute every time your schema is pushed.
sed -i.bak $'s/\t*//g' schema.sql

Error when SQL query called from C program

I want to execute SOURCE data/keyw.sql from C program. This query works fine when I execute from the command line but gives the following error on executing mysql_query(con, "SOURCE data/keyw.sql")
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 'SOURCE data/keyw.sql' at line 1
Any help would be highly appreciated.
The SOURCE command is not executed on the MySQL server. It is interpreted by the mysql Client, and is basically just a convenience.
Quote from MySQL documentation:
mysql sends each SQL statement that you issue to the server to be executed. There is also a set of commands that mysql itself interprets. For a list of these commands, type help or \h at the mysql> prompt:
Note that source is one of those listed. If you want to know how it works, then have a look at the source code for the mysql Client.
If your source to be executed contains more than one MySQL statement then your connection will need to support multiple statement execution, or alternatively you need to parse the SQL into individual statements yourself.

creating stored procedure in ubuntu terminal

I am trying to create stored procedures in mysql. I am doing it on terminal (ubunutu).
But when I am adding ; before end it executes all lines and throw an error .
I am trying to write
create procedure test()
-> begin
-> select * from salary;
I am unable to add end in last and it executeds and got this 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 '' at line 3.
Is this a right way to write stored procedure in terminal ?
Can I write stored procedure on separate file?
Then how can I execute that stored procedure written in file ?
fortunately i found a way to write a stored procedures on terminal
http://alextsilverstein.com/programming-and-development/mysql/mysql-the-reason-for-using-the-delimiter-statement-in-mysql-routines-stored-procedures-functions-triggers/

MySQL delimiter syntax error

I'm trying to change the MySQL command delimiter so I can create a procedure with multiple commands in it. However, the delimiter command does not seem to be recognised on MySQL 5.1.47. I tested it on MySQL 5.0.91, and it did work there.
DELIMITER //;
DELIMITER ;//
I'm trying to run this from phpmyadmin, in both situations. Using 5.0.91 instead isn't an option because I need to use events (CREATE EVENT).
Error message:
#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 'DELIMITER //' at line 1
Is there a reason it's not working, or is there an alternative to accomplish the same thing (creating a procedure with multiple queries)?
DELIMITER is not a MySQL command. It's a command that your MySQL client needs to support. I was running PHPMyAdmin 2.8.2.4, which didn't support it. When I upgraded to the newest version, which is currently 3.4.9, it worked just fine. Your MySQL version has nothing to do with DELIMITER and whether it's supported or not.
You don't need to delimit the DELIMIT statements
DELIMITER //
procedure here etc
DELIMITER ;
Exactly as per "Defining Stored Programs" in the MySQL docs.
And if you can control versions, the latest is 5.5.20. Why not use that?
Edit:
The error message indicates an error in the previous statement... if this can't be seem force it thus
; /* <- force previous statement termination */ DELIMITER //
procedure here etc
DELIMITER ;