I need to create a trigger in sql server via R code for which I need to set my sql delimiter to //.
I tried doing the following:
dbExecute(con, "delimiter //")
dbExecute(con, "delimiter //\n")
dbExecute(con, "delimiter //\t")
I also tried the above scenarios with other DBI functions like
dbGetQuery and dbSendQuery
but I am getting the following error.
could not run statement: 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
It turns out that in order to execute an sql trigger through R using the DBI package, one does not need to set and unset the delimiter. We can directly execute the trigger command.
This is unlike what needs to be done while setting a triggers through SQL command line where, since the trigger syntax itself includes a semicolon ;, in order to avoid conflict with the default SQL delimiter which is also ; we temporarily set the delimiter to a lesser used special character such as // with a command such as
delimiter //
and then revert back to the default delimiter with
delimiter ;
which need not be done when trigger is executed through DBI package of R.
Related
i wat to create this trigger to set a defaul value for a clomn but i get this message error : #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 '' at line 4
this is my script :
CREATE TRIGGER trg_set_content_val BEFORE INSERT
ON post_table
FOR EACH ROW BEGIN
set NEW.content = 'mu value here';
END;
You need to set the delimiter to something else than semicolon before the stored program and then change it back:
DELIMITER //
CREATE TRIGGER trg_set_content_val
BEFORE INSERT
ON post_table
FOR EACH ROW BEGIN
set NEW.content = 'mu value here';
END//
DELIMITER ;
Reason:
If you use the mysql client program to define a stored program
containing semicolon characters, a problem arises. By default, mysql
itself recognizes the semicolon as a statement delimiter, so you must
redefine the delimiter temporarily to cause mysql to pass the entire
stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The
following example shows how to do this for the dorepeat() procedure
just shown. The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
I was wondering if you can tell me what's wrong with the ff sql statement:
insert into translog
select * from transponder_logs where trans_log_id < 150000;
delete from transponder_logs where trans_log_id < 150000
This statement ran just fine in sql but it gives me a syntax error when I used it on event scheduler.
The error message was:
"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 'delete from transponder_logs where trans_log_id < 150000 at line 3"
Whenever you define code like routines that have multiple executable statements, you MUST define a custom DELIMITER. And your code will be sent to the server along with delimiter instruction. And the server compiles the code as a block before it finds the newly defined custom delimiter.
Read what documentation says:
Defining Stored Programs
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. .... The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.
I believe your event scheduler code is just executed as is without defining such delimiter.
Change it as following:
-- set the new delimiter
DELIMITER //
-- include your event scheduler code block here
-- lastly terminate the code block, with new delimiter
-- so that server starts compiling the code
//
-- now reset the delimiter to default
DELIMITER ;
Refer to: CREATE EVENT Syntax
when I change Delimeter from mysql console or MySQL Workbench I do not get any error,
but when I embed the same code in ruby on rails I get error
mysql> DELIMITER $$
mysql>
gives no error.
but
ActiveRecord::Base.connection.execute(%Q{
DELIMITER $$
})
gives:
ActiveRecord::StatementInvalid: Mysql2::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 $$' at line 1:
The top answer is correct (Rails cannot execute DELIMITER because it is a MYSQL command), but #ishandutta2007's follow up question was not answered, so I'll answer that here.
DELIMITER is often used to wrap mysql function and procedure bodies; to achieve this in rails simply wrap the procedure body in it's own execute statement.
So for instance code that might read like:
execute <<-SQL
DROP FUNCTION IF EXISTS MyFunc;
DELIMITER $$
CREATE FUNCTION My Func
. . .
$$
DELIMITER ;
SQL
Would instead become the following, with the multiple execute calls acting as the 'scoping' intended by redefining the delimiter:
execute 'DROP FUNCTION IF EXISTS MyFunc'
execute <<-SQL
CREATE FUNCTION My Func
. . .
SQL
DELIMITER is actually a MySQL command line setting, not SQL: http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html. That means that you can't set the delimiter in this way.
On top of that, it wouldn't help if you could as ActiveRecord::Base.connection.execute only allows you to execute one statement at a time out of the box (see http://www.seanr.ca/tech/?p=75).
I have tried to use the export option in phpMyAdmin routines panel to copy functions from one database to another, with no success.
The export option supplies me with the following:
CREATE DEFINER=`root`#`localhost` FUNCTION `JSON_FIELD_NUM`(`col_name` TEXT CHARSET utf8, `data` TEXT CHARSET utf8) RETURNS text CHARSET utf8
NO SQL
BEGIN
RETURN
CONCAT('"',col_name,'":',
IF(ISNULL(data),0,data)
);
END
I get this error when I run that in another database:
#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 7
I tried adding DELIMITER $$ at the top and $$ after END, but still no joy.
You must set your client's statement delimiter to a string other than ; in order that it doesn't think the semicolon which ends the CONCAT() expression also terminates the CREATE FUNCTION statement.
In the MySQL command line tool, you can use the DELIMITER command. In phpMyAdmin, you will need to use the Delimiter text box before clicking Go.
There is a far more concise way to do this:
MYSQLDUMP_OPTIONS="${MYSQLDUMP_OPTIONS} --routines"
MYSQLDUMP_OPTIONS="${MYSQLDUMP_OPTIONS} --all-databases"
MYSQLDUMP_OPTIONS="${MYSQLDUMP_OPTIONS} --no-data"
MYSQLDUMP_OPTIONS="${MYSQLDUMP_OPTIONS} --no-create-info"
mysqldump ${MYSQLDUMP_OPTIONS} > StoredProcedures.sql
less StoredProcedures.sql
This will dump only Stored Procedures.
Give it a Try !!!
So I have this stored proc that will not get created when I run the file.
DELIMITER //
DROP PROCEDURE IF EXISTS msd.test_proc//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM
msd.zipcode_lookup;
END//
DELIMITER ;
When I run this I get an error code 1064 at line 1 when I execute in RazorSQL. Here is the complete error message:
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 '//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM ' at line 1
Error Code:1064
I've tried other variations and still get errors. I am sure this is something basic I am missing. I appreciate any help.
Thanks.
As stated on the RazorSQL website:
The DELIMITER statement is not part of the MySQL language. It is a command supported by certain MySQL tools. This command tells those MySQL programs to scan for a certain character that indicates the end of a query or statement.
RazorSQL does not support using the DELIMITER command. The SQL statement delimiter value used by RazorSQL can be changed using the preferences window. The default values is the semi-colon.