MySQL DELIMITER syntax errors - sql

This MySQL script installs multiple triggers.
It works on one machine running MySQL 5.0.51b-community. On another machine running MySQL 14.12 Distrib 5.0.45, for redhat-linux-gnu (i386) it fails, with this error message, which seems to be related to the DELIMITER // ... // DELIMITER; syntax :
ERROR 1064 (42000) at line 272: 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; DROP TRIGGER IF EXISTS trigger_name; DELIMITER' at line 1
The script syntax (summarised) is:
DROP TRIGGER IF EXISTS trigger_name;
DELIMITER //
CREATE TRIGGER trigger_name BEFORE UPDATE ON table
FOR EACH ROW BEGIN
-- Trigger logic goes here
END //
DELIMITER;
-- More trigger drop/create statements follow
What is wrong with the script, and how can I correct it?

Try
DELIMITER ;
not
DELIMITER;
You're actually specifying ; as an argument to the DELIMITER command, so not having the space there may be confusing it.

You need a space between 'DELIMITER' and ';'
DELIMITER ;
# not:
DELIMITER;

Just as an add-on, for someone else:
The delimiter is required to enable the entire definition to be passed to the server as a single statement.

Try the below.
I am sure it should solve the purpose.
DELIMITER +
CREATE TRIGGER STUDENT_INSERT_TRIGGER BEFORE INSERT ON FSL_CONNECTIONS
FOR EACH ROW BEGIN
INSERT INTO STUDENT_AUDIT
SET STUDENT_ID = NEW.STUDENT_ID,
MAC_ADDRESS = NEW.MAC_ADDRESS,
IPADDRESS = NEW.IPADDRESS,
EMAIL_ID = NEW.EMAIL_ID ,
START_TIME=NEW.START_TIME,
END_TIME=NEW.END_TIME,
STATUS=NEW.STATUS;
END; +
From the above when we use a DELIMITER. It should be in the form of
DELIMITER +
--
BLOCK OF SQL WHATEVER YOU WANT TO MENTION
--
+

In the version of MySql I use the same error occurs when using the delimiter command, but this version handles the delimiter ";" for statements and delimiter "|" for stored procedures and functions, which i think solves the problem; try this:
DROP TRIGGER IF EXISTS trigger_name;
CREATE TRIGGER trigger_name BEFORE UPDATE ON table
FOR EACH ROW BEGIN
-- Trigger logic goes here
END |
-- other statements or functions here

Hmm I'm having similar problems. I do a mysqldump from Debian Lenny running 5.0.51 and try importing to OpenSolaris running 5.0 and get the same error. And I have DELIMITER ;
Version conflict?

Related

Error when creating a stored procedure in MariaDB

I am trying to create a stored procedure in MariaDB, and I keep getting the 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 'END' at line 9
DELIMITER $$
Create procedure patientcheck(IN hospitalcode int(11))
BEGIN
Select Staff1.First_name, Staff1.Surname, Staff1.Dep1
From Patient1, Staff1, Staff_allocator
Where Patient1.PatientID1=Staff_allocator.PatientID1 and Staff1.StaffID1=Staff_allocator.StaffID1
And Patient1.PatientID1=hospitalcode
END $$
DELIMITER ;
any help will be appreciated thanks
im not sure how or why my issue was solved, but after rewriting my code character by character, even without changing anything ( and re adding the ";"), the code finally ran. (ive been told that phpmyadmin is very buggy and unstable and is prone to silly issues like this)
You are missing a semicolon at the end of your select statement. You changed the delimiter to $$, but you still need to add the semicolon to the end of the query. I ran this exact code on a MariaDB instance with the semicolon added and it ran without error.

DB2 stored procedure gave syntax errors

I am creating a stored procedure for db2. But it is giving an error saying that
"SQL Error [42601]: An unexpected token "END-OF-STATEMENT" was found
following "SS_TOKEN_BAK". Expected tokens may include: " END IF"..
SQLCODE=-104, SQLSTATE=42601, DRIVER=4.23.42".
Following is my stored procedure.
CREATE OR REPLACE PROCEDURE TOKEN_CLEANUP_SP
BEGIN
DECLARE batchSize INTEGER;
-- ------------------------------------------
-- CONFIGURABLE ATTRIBUTES
-- ------------------------------------------
SET batchSize = 10000; -- SET BATCH SIZE FOR AVOID TABLE LOCKS [DEFAULT : 10000]
-- ------------------------------------------------------
-- BACKUP IDN_OAUTH2_ACCESS_TOKEN TABLE
-- ------------------------------------------------------
IF EXISTS (SELECT TABLE_NAME FROM TABLES WHERE TABLE_NAME = 'IDN_OAUTH2_ACCESS_TOKEN_BAK')
THEN
DROP TABLE IDN_OAUTH2_ACCESS_TOKEN_BAK;
END IF;
END/
Is anyone face this type of issue. Any help on this would be much appreciated.
Verify that you have the end-of-statement delimiter configured correctly for whatever tool submits the 'CREATE OR REPLACE' procedure. Different tools have different ways to configure the block terminator (alternatively known end of statement delimiter). For command line scripts, use --#SET TERMINATOR / at the start of the file, other ways are possible.
Your code sample shows / as the block terminator, so you might want to use that character as the block delimiter. The semi-colon terminates statements inside the block.
Separately you should see that your code won't compile if the specified table does not exist in the implied schema at compilation time, because you are using static SQL. You may want to use dynamic SQL instead for the drop table statement (search for 'EXECUTE IMMEDIATE' examples).

Error while creating procedure in db2

I got error while creating procedure in db2.
Error is - Expected tokens may include: psm_semicolon.. SQLCODE=-104
Help Me....
CREATE PROCEDURE update_new()
LANGUAGE SQL
BEGIN
CREATE TABLE TEMP(METADATA_KEY varchar(40),NEW_METADATA_KEY varchar(40));
END;
In whatever tool you're using, change the statement terminator to something other than semicolon and put that terminator at the end of the CREATE PROCEDURE statement.
For example, if using the command line processor, save this to a file (note the "#" symbol at the end:
CREATE PROCEDURE update_new()
LANGUAGE SQL
BEGIN
CREATE TABLE TEMP(METADATA_KEY varchar(40),NEW_METADATA_KEY varchar(40));
END#
then execute the file: db2 -td# -f myproc.sql
The reason for doing this is that semicolons are always used as terminators within the procedure code, so you must use something else to terminate the CREATE PROCEDURE statement.

Compound Statement unable to change the delimiter

I am attempting to run a simple compound statement within the Query Editor of DB Solo 4.2.2
It appears I am unable to properly change the end of line delimiter. I am using DB2. Here is a simple example that gives the error:
--#SET TERMINATOR #
BEGIN ATOMIC
DECLARE id INT;
SET id = 10;
END #
--#SET TERMINATOR ;
Error is:
An unexpected token "INT" was found following "N ATOMIC DECLARE id". Expected tokens may include: "END-OF-STATEMENT"
Thanks in advance
DB2 only allows the semicolon to be used as a delimiter in Compound SQL. The syntax you are using appears to only be valid when using the db2batch utility (which comes with DB2 Linux/Unix/Windows).
Here is some relevant information from the Information Center (this is from the z/OS IC):
How to code multiple statements in an SQL procedure
Use a semicolon
character to separate SQL statements within an SQL procedure.
The procedure body has no terminating character. Therefore, if the
procedure contains only one statement, you do not need to put a
semicolon after that statement. If the procedure consists of a set of
nested statements, you do not need to put a semicolon after the
outermost statement.

What does DELIMITER // do in a Trigger?

DELIMITER //
What is the of use of it?
It changes the statement delimiter from ; to //. This is so you can write ; in your trigger definition without the MySQL client misinterpreting that as meaning you're done with it.
Note that when changing back, it's DELIMITER ;, not DELIMITER; as I've seen people try to do.
In SQL you close each statement with a delimiter, which is by default a semicolon (;). In a trigger you need to write multiple statements, each ending in a semicolon. To tell MySQL that those semicolons are not the end of your trigger statement, you temporarily change the delimiter from ; to //, so MySQL will know that the trigger statement only ends when it econunters a //.
Add an example:
When working with mysql shell command, we used ; delimiter to close each statement. However, in case, we want to build the store procedures and triggers, we need to add the semicolon ; in these statements also.
delimiter //
create trigger log_students after insert on students
for each row
begin
insert into log_students(change_by, change_at) values(USER(), NOW());
end//
delimiter ;
Simple sets the end of statement delimiter (; semi-colon in standard, default SQL).
Changing the character could be useful if you want to use ; in your SQL, or you are using embedded SQL (where it might lead to confusion). similarly the // in your example could lead to confusion in embedded SQL, or you might want to use it in your SQL. So imply use DELIMITER to set the delimiter that is appropriate for your application and needs.
Read the (ummmm) mysql documentation.
delimiter is the marker for the end of each command you send to the mysql command line client.
delimiter is not only related to triggers, but defining triggers and stored procedures is one strong use case as you wish them to contain semicolons (;) which are otherwise the default delimiter.