MySQL Syntax Error - sql

When executing:
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 1;
END;
DECLARE EXIT HANDLER FOR SQLWARNING
BEGIN
ROLLBACK;
SELECT 1;
END;
-- delete all users in the main profile table that are in the MaineU18 by email address
DELETE FROM ap_form_1 WHERE element_5 IN (SELECT email FROM MaineU18);
-- delete all users from the MaineU18 table
DELETE from MaineU18;
COMMIT;
END;
I get:
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 'e1:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK' at line 2
Any ideas? Thanks.
UPDATE 2:
I have tried putting the script into a PROCEDURE:
DELIMITER |
DROP PROCEDURE IF EXISTS temp_clapro|
CREATE PROCEDURE temp_clapro()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING ROLLBACK;
SET AUTOCOMMIT=0;
-- delete all users in the main profile table that are in the MaineU18 by email address
DELETE FROM ap_form_1 WHERE element_5 IN (SELECT email FROM MaineU18);
-- delete all users from the MaineU18 table
DELETE from MaineU18;
COMMIT;
SET AUTOCOMMIT=1;
END
|
DELIMITER ;
CALL temp_clapro();
I am still having issues:
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (2.40 sec)
Query OK, 0 rows affected (2.40 sec)
Query OK, 0 rows affected (2.40 sec)
Query OK, 0 rows affected (2.40 sec)
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 'END;
|
DELIMITER ;
CALL temp_clapro()' at line 1
UPDATE 3:
It seems that many of my problems are coming from the fact that I am running the script from a file using the "SOURCE" command. If I only have the DROP and CREATE commands in the file and run the DELIMITER and CALL commands outside the file, everything works without error.
Is there away to run this from a single script file?

You seem to be using BEGIN as the opening of a block of ad hoc statements, as one would do in SQL Server.
MySQL doesn't support this. You can DECLARE only in the body of a stored procedure or stored function or trigger.
http://dev.mysql.com/doc/refman/en/declare.html:
DECLARE is allowed only inside a BEGIN ... END compound statement and must be
at its start, before any other
statements.
http://dev.mysql.com/doc/refman/en/begin-end.html:
BEGIN ... END syntax is used for
writing compound statements, which can
appear within stored programs.
Re your comments and updated question: I don't know why it's failing. I just tried it myself and it worked fine. What version of MySQL are you using?

you're using semicolons with the procedure for statement delimiters, so you have to change the delimiter in your client. see http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html

Related

Cannot create trigger if-delete-clause

Here is the code I have
CREATE TRIGGER free_tokens AFTER INSERT ON `csrf_token`
IF (SELECT COUNT(`csrf_token`.`id`)) > 5000 THEN
DELETE FROM `csrf_token` WHERE `csrf_token`.`time` < (UNIX_TIMESTAMP(NOW() - INTERVAL 2 HOUR))
END IF;
which checks if there is more than 5000 entries after an insert, then deletes all entries which are greater than 2 hours old
I am getting you have an error in your mysql syntax near IF (SELECT ... DELETE FROM ...
I am using MariaDB, can someone help me understand where is the error in this simple statement?
There are small things that your trigger definition is missing:
change the MariaDB delimiter type into '//', so that the trigger instructions can be separated by the semicolon instead
FOR EACH ROW, as part of MariaDB trigger syntax
your DELETE statement is missing a semicolon at the end
can't nest queries inside an IF-THEN-ELSE statement
END to delimit the end of the trigger after its definition, as part of MariaDB syntax
A workaround for the nested query in your IF statement can be to define a variable to be updated with the result of a SELECT, to be carried out before the IF statement.
DELIMITER //
CREATE OR REPLACE TRIGGER free_tokens
AFTER INSERT
ON `csrf_token`
FOR EACH ROW
BEGIN
DECLARE num_rows INT;
SELECT
COUNT(*) INTO num_rows
FROM
`csrf_token`;
IF num_rows > 5000 THEN
DELETE FROM
`csrf_token`
WHERE
`csrf_token`.`time` < (UNIX_TIMESTAMP(NOW() - INTERVAL 2 HOUR));
END IF;
END//
DELIMITER ;
More on MariaDB trigger syntax here.

PLS-00103: Encountered the symbol "CREATE" when expecting one of the

I am trying to create a stored procedure in oracle 12c database and I am getting error when I am running code to store the procedure.
PLS-00103: Encountered the symbol "CREATE" when expecting one of the
There are multiple stack overflow question already asked on this topic. but they suggest some different syntax. which is deviation from actual oracle documentation. and even those didn't worked for me
I checked for documentation on multiple website including oracle documentation. oracle documetation suggest syntax as following
Oracle STORED PROCEDURE DOCUMENTATION
so I as per the syntax I wrote the following procedure.
CREATE PROCEDURE PDD_PROC_BASE
AS
--DROP TABLE BASE;
CREATE TABLE BASE as
SELECT idno
,DATE
,diff
,SUBSTR(idNO,7,2) AS PRD
,COMPLETED
,CATG
,OP_Number
FROM table1
WHERE = date >= '30-JUN-2018'
AND STATUS = 'G'
END;
and I got the following error.
Procedure PDD_PROC_BASE compiled
Errors: check compiler log
Errors for PROCEDURE AN_5043152.PDD_PROC_BASE:
LINE/COL ERROR
-------- ------------------------------------------------------------------------------
7/5 PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
json_exists json_value json_query json_object json_array
I checked other resources as well but still didn't understood what went wrong.
I even tried code example from Oracle documentation and got similar error.
I am using SQLdeveloper tool as client
You cant use directly sql ddl statements in plsql block, you can do the same thing using dynamic sql with the "EXECUTE IMMEDIATE" statement like this:
begin
execute immediate 'create table test_table1 (test_column1 varchar2(40))';--your create table statement here
end;
In Oracle in stored procedures you can use DDL statements only as dynamic SQL, that means as EXECUTE IMMEDIATE 'CREATE TABLE ' ...
Check here, for example: http://www.dba-oracle.com/t_using_ddl_create_index_table_plsql.htm

Mysql Create Insert Procedure Statement incomplete

I'm trying to wirte a little log procedure for my database. I create a procedure with this statment:
create procedure prc_wirte_log (
in p_schema varchar(255),
in p_item varchar(255),
in p_message varchar(255)
)
begin
insert into weather.log (`schema`, item, message) values (p_schema, p_item, p_message);
end;
I get the error Error Code: 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 7 0.063 sec
Why? The MySQL Workbench means Incomplet Statment: excepting ; after the insert query.
What could I do?
Multistatement procedures (assumed when BEGIN...END is present) require delimiter overrides to prevent the statements they contain from terminating the procedure definition prematurely.
Typically, you need to do something like:
DELIMITER //
CREATE PROCEDURE blah()
BEGIN
statements;
END//
DELIMITER ;
The first example on the documentation here demonstrates this (though the last two on that page seem to repeat your mistake.
If you are using WorkBench or similar tool just right click on StoredProcedures and click Create stored procedure the tool will create default structure like below and you could write your logic and hit on apply. Ensure to use semicolon at the end of the last statement (just before END).
CREATE PROCEDURE `new_procedure` ()
BEGIN
select * from tasks;
END

Error: ORA-00969: missing ON keyword - Oracle

I am using Oracle 11g XE database and Oracle SQL developer to execute SQL statements.
I have this SQL statement which is giving me the above compiler error when executing it.
CREATE OR REPLACE
TRIGGER "STD"."TRG_STUDENT"
BEFORE INSERT,DELETE
ON STUDENT
FOR EACH ROW
BEGIN
IF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Inserting !!');
END IF;
IF DELETING THEN
DBMS_OUTPUT.PUT_LINE('Deleting !!');
END IF;
END;
I tried some variations but I used to get other errors.
I placed the ON STUDENT just before the BEFORE INSERT,DELETE line and I get this error:
Error: ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword
What am I missing here?
BEFORE INSERT OR DELETE
More about Create Trigger syntax: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7004.htm
Use BEFORE INSERT OR DELETE instead of BEFORE INSERT, DELETE. Refer coding trigger for more in detail.

Is something wrong with this MySQL query?

I'm writing stored procedures for the first time, and it's not working. The trouble is I can't see a reason for it not to work.
I'm running it through phpmyadmin 2.8.2.4. My MySQL version is 5.1. Here is the first part of the query:
create procedure under_user_type (in original_post int, out user_type int, out user_id longtext)
begin
if exists (
select *
from wp_postmeta as pm
where pm.post_id = original_post
and pm.meta_key = '_tdomf_original_poster_id'
) then
set user_type = 0;
select pm.meta_value
into user_id
from wp_postmeta as pm
where pm.post_id = original_post
and pm.meta_key = '_tdomf_original_poster_id';
elseif exists ( ...
I get the error:
#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 9
Line 9 corresponds to that first select statement in the if exists ( ... ) then portion.
update:
I get the same error if I use:
create procedure under_user_type (in original_post int, out user_type int, out user_id longtext)
begin
if 1=1 then begin
set user_type = 0;
select pm.meta_value
into user_id
from wp_postmeta as pm
where pm.post_id = original_post
and pm.meta_key = '_tdomf_original_poster_id';
end;
Update Again:
Running the examples on this MySQL documentation page also gives me the "check your syntax near ''" error. I tried removing all tabs from the query but that did nothing.
Update a third time:
I can run this query:
create procedure blah()
begin
end;
but not this query:
create procedure blah2()
begin
if 1=1 then
begin
end;
end if;
end;
as I get the same error.
The following works for me:
create procedure blah2()
begin
declare s int;
if exists(select 1) then
set s=1;
end if;
end;
I think your problem (in the very first example, before you try doing all of your experimentation) is that you didn't declare the variable user_type, so MySQL prints out a (very generic) error when it encounters a variable name that it's never seen before on line 9.
(As for all of your other examples, you should not do if ... then begin ... end; The proper syntax is if ... then ... elseif ... else ... end if)
The error is on line 9, when it encounters the first semi-colon.
You need to set the DELIMITER to something other than semi-colon if you're going to use semi-colons inside the body of your procedure.
Here's how to do that for your simplest example:
DELIMITER $$
create procedure blah2()
begin
if 1=1 then
begin
end;
end if;
end $$
DELIMITER ;
The problem is that PHPMyAdmin must be trying to split up the queries itself every time it sees a semicolon. This is what the MySQL CLI does, which is why it's necessary to change the delimiter when creating a stored procedure on the command line. However, my version of PHPMyAdmin (2.8.2.4) doesn't allow me to change the delimiter, so I just wound up with a bunch of unhelpful error messages.
I wound up writing a script on the remote server with a textarea and submit button that would pass the contents of the textarea to mysqli::multi_query. This function lets mysql handle the delimiters rather than trying to split it up itself, and so creating the procedure worked in this respect.
Another, easier route would be to use MySQL Workbench to connect to the database, possibly through ssh if the database only allows connections from localhost.
I don't think MySQL can handle "if exists".
You could try
declare testMe integer default (select ID from ... where ...);
if testMe is not NULL then
...