I want run this query but get an error:
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 'UPDATE `ads` SET `aDesc` = replace(aDesc, 'amp;', '')' at line 3
My query is:
UPDATE `ads`
SET `aName` = replace(aName, 'amp;', '')
UPDATE `ads`
SET `aDesc` = replace(aDesc, 'amp;', '');
What's the problem?
Your query looks like two queries without a separating delimiter.
The more efficient option is to do both changes in one query:
UPDATE ads
SET aName = replace(aName, 'amp;', ''),
aDesc = replace(aDesc, 'amp;', '');
but if you must run two queries:
UPDATE ads SET aName = replace(aName, 'amp;', '');
UPDATE ads SET aDesc = replace(aDesc, 'amp;', '');
Related
I need to run a prepared statement in Liquibase:
<changeSet id="53" author="foo">
<sql dbms="!h2, mysql" splitStatements="true">
SET #table_name = 'AUDIT_EVENT';
SET #column_name = 'ACTOR_ID';
SET #constraint_name = (
SELECT rc.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS rc
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu
ON kcu.CONSTRAINT_CATALOG = rc.CONSTRAINT_CATALOG
AND kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
WHERE kcu.TABLE_NAME = #table_name
AND kcu.COLUMN_NAME = #column_name
);
SET #s = concat('ALTER TABLE ', #table_name, ' DROP FOREIGN KEY ', #constraint_name);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt
</sql>
</changeSet>
If I set splitStatements to true, then I think parameters don't persist across statements so understandably I get an error saying that #s is NULL:
Reason: liquibase.exception.DatabaseException: 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 'NULL' at line 1 [Failed SQL: PREPARE stmt FROM #s]
If I set splitStatements to false, and also delete all but the first two SETs to make sure it's not the nested SELECT causing the issue, I get this:
Reason: liquibase.exception.DatabaseException: 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 'SET #column_name = 'ACTOR_ID'' at line 2 [Failed SQL: SET #table_name = 'AUDIT_EVENT';
SET #column_name = 'ACTOR_ID';]
I assumed then the issue with with the endDelimiter defaulting to ;. So I tried FOO:
<sql dbms="!h2, mysql" splitStatements="false" endDelimiter="FOO">
SET #table_name = 'AUDIT_EVENT';
SET #column_name = 'ACTOR_ID';
FOO
</sql>
To which I get...
Reason: liquibase.exception.DatabaseException: 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 'SET #column_name = 'ACTOR_ID';
FOO' at line 2 [Failed SQL: SET #table_name = 'AUDIT_EVENT';
SET #column_name = 'ACTOR_ID';
FOO]
I feel like I don't understand what's going on. I checked SQL does work in MySQL Workbench. Any insights?
Update
So, I thought splitStatements=false got me past the NULL #s error, and therefore hypothesized that parameters (or "user/session variables" as I now know to call them) must not persist when split. But that was all wrong. Those variables do persist across statements: for the duration of the session.
splitStatements=false simply masked the original error with a new one, which did indeed have to do with MySQL not allowing multiple queries at once. When I unblocked that using allowMultiQueries=true, I got a new error pointing to Line 1, which eventually got me to fix the original error, which was that my table name was wrong (uppercase instead of lowercase).
What I never realized was that, the table name being wrong also contributed to #s ending up NULL.
Old "Solution"
I didn't realize that MySQL doesn't allow multiple queries by default. I had to add that in the JDBC URL as a query parameter:
jdbc:mysql://%s:%d/%s?user=root&allowMultiQueries=true
As I understand, this is not a portable path. But since I'm using Liquibase to specify exactly what database to run these statements on, I think it's okay.
I have tried the following:
use my_db;
SET SESSION query_cache_type=OFF;
SET #CA_FR_ID = '16';
SET #CA_EN_ID = '15';
SET #US_ID = '1';
select * from my_table where my_table.id in (#CA_FR_ID, #CA_EN_ID);
and...
use my_db;
SET #CA_FR_ID = '16';
SET #CA_EN_ID = '15';
SET #US_ID = '1';
select * from my_table where my_table.id in (#CA_FR_ID, #CA_EN_ID);
and...
use my_db;
SET #CA_FR_ID := '16';
SET #CA_EN_ID := '15';
SET #US_ID := '1';
select * from my_table where my_table.id in (#CA_FR_ID, #CA_EN_ID);
and...
use my_db;
SET #CA_FR_ID = '16',
#CA_EN_ID = '15',
#US_ID = '1';
select * from my_table where my_table.id in (#CA_FR_ID, #CA_EN_ID);
Each of these results in an error thrown after the first semicolon after SET:
[42000][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 'SET
This is strange because I have used this syntax before. And, even copying/pasting working code from the past is now returning this error.
EDIT:
I suppose I must have corrupt a MySQL setting in an earlier variable set attempt. Restarting MySQL allowed me to execute the queries in my above examples.
When I run this sql command:
UPDATE chat_data
SET message = replace(message, '\', '')
LIMIT 1 ;
It gives me syntax error:
13:07:46 UPDATE chat_data SET message = replace(message, '\', '')
LIMIT 1 ; Error Code: 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 ''\', '') LIMIT 1' at line 1 0.237 sec
Any solution for this ?
You need to escape the \ character:
UPDATE chat_data SET message = replace(message, '\\', '') LIMIT 1 ;
I had a case today on a legacy project.
Here is a solution for a SELECT statement :
'SELECT * FROM my_table WHERE label = REPLACE("'.mysql_real_escape_string($label).'", "'.mysql_real_escape_string('\\').'", "")'
If you want to replace visible backslash (\) with sql you could also use
REPLACE(message, CHAR(92), '')
I found here a topic about an MySQL IF, ELSE query,i adapted it but i can't figure it out what is the problem with it.
Here is the query:
IF (SELECT * FROM `jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') IS NULL THEN
INSERT INTO `jos_import03_07_2011` (`tip_imp`, `tip_produs`, `producator`,
`cod_intern`, `desc`, `cod_oem`, `pret`, `valuta`) VALUES ('Imprimanta Laser',
'Piese Schimb', 'BROTHER', 'BR-200503', '', 'OP-4CL', '338.49', 'EUR');
ELSE UPDATE `jos_import03_07_2011` SET `pret` = '338.49' WHERE `cod_oem` = 'OP-4CL';
END IF;
And here is the 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 'IF (SELECT * FROM
`jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') IS NULL THE' at line 1
This is the original post:
Conditional mySQL statement. If true UPDATE, if false INSERT
Thanks,
Sebastian
UPDATE
Error code for IF EXISTS:
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 'IF EXISTS (SELECT * FROM
`jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') THEN' at line 1
Any reason you can't use the INSERT ... ON DUPLICATE KEY syntax?
INSERT INTO `jos_import03_07_2011` (`tip_imp`, `tip_produs`, `producator`,
`cod_intern`, `desc`, `cod_oem`, `pret`, `valuta`)
VALUES ('Imprimanta Laser', Piese Schimb', 'BROTHER', 'BR-200503', '', 'OP-4CL', '338.49', 'EUR')
ON DUPLICATE KEY UPDATE SET pret = VALUES(pret)
would be far more efficient: one less query and far less code to debug.
i have an error in my stored-procedure. I use MySql DB
SET #counter = 1;
SET #last = 0;
UPDATE Customer SET ordre = (IF(#last = customer_id,#counter + 1,#counter = 1)),
#last = customer_id
My Error
Script line: 3 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 '#last = customer_id ORDER BY
customer_id' at line 2
You cannot set variables in SET clause of UPDATE statement. '#last = customer_id' causes the error.
From the reference -
UPDATE syntax - '...SET col_name1=expr1 [, col_name2=expr2 ...]'
The SET clause indicates which columns to modify and the values they should be given.