Multipe Update statements - MariaDB [duplicate] - mysql

This question already has answers here:
Multiple SQL query not working with delimiter on DBeaver
(3 answers)
Closed 7 months ago.
I use dbeaver to connect to a Mariadb. When I try to run a multi-line update statements, e.g.
update x set warehouse_id='WH02' where soh_id='f0b4d220';
update x set warehouse_id='WHU1' where soh_id='17482705';
I get the error 'Caused by: java.sql.SQLSyntaxErrorException: 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 '...''. If I run the same queries against the database connection created in MySQL workbench, it runs without any problem.
I assume that there is an issue with the usage of ';' but I cannot find a way of running multiple update statements in Mariadb. What am I doing wrong?

To do a batch update in dbeaver, one needs to run script by selecting the queries and running Alt+X.

You could try this:
update x
set warehouse_id = case soh_id
when 'f0b4d220' then 'WH02'
when '17482705' then 'WHU1'
end
where soh_id in ('f0b4d220', '17482705')

when you have a unique key on the field soh_id you can use the INSERT INTO .... ON DUPLICATE KEY statement like this.
INSERT INTO x (soh_id, warehouse_id)
VALUES
('f0b4d220','WH02')
,('17482705','WHU1')
ON DUPLICATE KEY UPDATE warehouse_id = VALUES(warehouse_id;

Related

My SQL Syntax error for multiple commands in one query, working for each command running separately

I'm trying to run the following MySQL command:
USE database_name;
DROP TEMPORARY TABLE IF EXISTS only_with_balance;
DROP TEMPORARY TABLE IF EXISTS keys_to_match;
CREATE TEMPORARY TABLE only_with_balance as (
SELECT
*
FROM
transactions t
WHERE
t.balance is not NULL
and (t.transaction_status_id = 4 or t.transaction_status_id = 5)
and (t.date between "2022-05-01" and "2022-08-24" )
);
But I'm getting a syntax error while trying to run the all the commands at once.
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 'DROP TEMPORARY TABLE IF EXISTS only_with_balance;
DROP TEMPORARY TABLE IF EXIST' at line 2
When I run each command separately, the result is the expected.
Can someone help me here?
What am I forgetting?
In MySQL, by default the query interface only allows one SQL statement per call.
There's an option to enable multi-query per call, but it must be set at connect time. Some MySQL connectors do this by default, or allow it as an option, but some do not. You didn't say if you're writing code or if you're submitting this set of queries through a client (though you tag the question 'dbeaver' you don't say anything else about that). So I can't guess what interface you're using for these queries.
Anyway, there's no advantage to using multi-query. The default mode is one SQL statement per call. That's what I do.
Using the default mode of a single SQL statement per call has some advantages:
Supports prepared statements and bound parameters (you can't run multiple statements in a single prepare call, even if you enable multi-query).
Simplifies processing errors and warnings.
Simplifies processing result sets.

mysql insert query is syntactically correct but showing errors [duplicate]

This question already has answers here:
#1064 -You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
(6 answers)
Closed 1 year ago.
INSERT INTO AlbumSongs (albumId,songId,rank) VALUES ("a3092aee5d397fef823656c31cd131e0","DPEUD1tm1RWp",9)
I tried to execute this query in my mysql database.
but it shows this 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 'rank) VALUES ("a3092aee5d397fef823656c31cd131e0","DPEUD1tm1RWp",9)' at line 1
my mysql server version ```Ver 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))``
What is wrong with this query? how can I solve this?
rank is a function in MySQL, so the query engine is expecting to see something like:
RANK() OVER (PARTITION BY {column} ORDER BY {column})
If you really must use this name for the column, consider putting all of your column names in back ticks like so:
INSERT INTO `AlbumSongs` (`albumId`, `songId`, `rank`)
VALUES ('a3092aee5d397fef823656c31cd131e0', 'DPEUD1tm1RWp', 9);
Note: Table names can also be wrapped the same way, and the strings are single-quoted rather than double quoted.
Ideally all table and column names should be defined like this when writing queries.

Should I handle ER_DUP_ENTRY before or after INSERT?

I have Node.js backend application running with MySQL.
I am handling ER_DUP_ENTRY on Node.js side, after INSERT query executed on current version of server. So I am running INSERT and then if MySQL returns that ER_DUP_ENTRY, I show up a warning to the user.
I thought getting this error every time from MySQL put an extra load on the database.
My question is, should I have to check database with SELECT for DUPLICATE entry and then execute INSERT query or is there no problem with the current version?
You don't want to check before executing the insert query. Just set it up UNIQUE constraint over the table for a specific column or combined column(multiple). So whenever you will insert the same data again MySQL will handle this duplicacy.
check the insert query response status. Then you can determine this is success / failed(duplicate or some other error).

Update else INSERT in mysql

I tested this code:
UPDATE books SET price='20000' WHERE user_id='2'
IF ROW_COUNT()=0
INSERT INTO store_books(name,user_id) VALUES ('test1','2')
I encountered the following error.
error : 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 'IF ROW_COUNT()=0 INSERT INTO store_books(name,user_id) VALUES
('test1','2')' at line 2
Is there any solution to solve this problem?
I don't want to use INSERT INTO ... ON DUPLICATE KEY UPDATE (because i have multi keys in my main table).
Above example is trial for finding new way.
Pattern that i want:
Update(if exists) ELSE Insert.
Two things:
INSERT ... ON DUPLICATE KEY UPDATE ... is by far the best way to do what you want to do. If you don't do this, you'll have to use a database transaction to make sure you maintain integrity for the operation you want.
MySQL (unlike, say, MS SQL server) doesn't allow conditional execution of queries except in stored procedures. Read this. If conditional in SQL Script for Mysql

How can I got clear the error while running UPDATE query in MySQL? [duplicate]

This question already has answers here:
MySQL error code: 1175 during UPDATE in MySQL Workbench
(25 answers)
Closed 8 years ago.
I am using MySQL WORKBENCH 6.1.6. I need to update my database_table. I have tried the following coding:
UPDATE `test`.`festexplorer_users`
SET
User_preference = 'All_Over_India'
WHERE User_year_of_passing=2015;
Here, "test" is my database name, festexplorer_users is my table name and I need to update the column User_preference. I have got the following error.
Error Code: 1175. You are using safe update mode and you tried to update a table without a
WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL
Queries and reconnect.
How could I clear my error?
In your MySQL WorkBench go to
Edit -> Preferences -> SQL Queries
Uncheck Forbid UPDATE and DELETE
statements without a WHERE clause (safe updates)
then
Query --> Reconnect to Server