Query always returns 1064 syntax error - mysql

I use 'MariaDB 5.5 x64' and Client HeidiSQL.
server environment is Windows Server2012 Datacenter.
And database use_progress a row is following
int id //auto-incremental primary key
int owner //owner user's unique id
varchar[20] name //key
int value //value
it stores online game user's states key-value type
for example
id owner name value
856 656 stage0cleared 0
857 656 have_gold 10214
858 657 inventory 22
and the next query's working test is fine
select count(*) from use_progress where owner = 656 and name = "inventory";
INSERT INTO use_progress (use_progress.owner, use_progress.name, use_progress.value) VALUES (656, 'inventory', 7);
UPDATE use_progress SET use_progress.value = 7 WHERE use_progress.`owner` = 656 AND use_progress.`name` = 'inventory';
but the next query is error 1064 (syntax error)
BEGIN
IF ((select count(*) from use_progress where owner = 656 and name = "inventory") = 0 ) THEN
INSERT INTO use_progress (use_progress.owner, use_progress.name, use_progress.value) VALUES (656, 'inventory', 7);
ELSE
UPDATE use_progress SET use_progress.value = 7 WHERE use_progress.`owner` = 656 AND use_progress.`name` = 'inventory';
END IF;
END
the error is folling next(always that error):
/* SQL 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 'IF ((select count(*) from use_progress where owner = 656 and name = "inventory")' at line 2 */
I tried everything I could. Use () or not, insert dbname.tablename. prefix to every column name or not. But in every situation, the same error occurs.
I even tried this (line 2 is changed):
BEGIN
IF (1>2) THEN
INSERT INTO use_progress (use_progress.owner, use_progress.name, use_progress.value) VALUES (656, 'inventory', 7);
ELSE
UPDATE use_progress SET use_progress.value = 7 WHERE use_progress.`owner` = 656 AND use_progress.`name` = 'inventory';
END IF;
END
But same error occurs (message is same)
I don't know why this happen.

You can't use control structures like IF() THEN ... in simple queries, only in stored procedures or functions.
In this case you'd use a stored procedure. Try like this:
DELIMITER $$
CREATE PROCEDURE my_proc_name(IN p_owner int, IN p_name varchar(50), IN p_value int)
BEGIN
IF NOT EXISTS (select 1 from use_progress where owner = p_owner and name = p_name) THEN
INSERT INTO use_progress (owner, name, `value`) VALUES (p_owner, p_name, p_value);
ELSE
UPDATE use_progress SET `value` = p_value WHERE `owner` = p_owner AND `name` = p_name;
END IF;
END $$
DELIMITER ;
After creating it, you'd call it like this:
CALL my_proc_name(656, 'inventory', 7);

Related

Creating a trigger to add new row to another table after new record using if statement

I cannot seem to get this trigger to work. I keep getting this 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 6
SELECT * FROM website_queries;
CREATE DEFINER=`name`
TRIGGER `add_subscribed_users`
AFTER INSERT ON `website_queries` FOR EACH ROW
BEGIN
IF NEW.subscribed = 0 THEN
INSERT INTO users SET id=DEFAULT, firstName = NEW.firstName, lastName = NEW.lastName, mobile=DEFAULT, email = NEW.email, admin=DEFAULT, createdAt = NEW.createdAt, subscribed = NEW.subscribed;
END IF;
END; //
I am using 10.5.15-MariaDB-cll-lve doing this through phpMyAdmin
You do not need in IF.
CREATE DEFINER=`name` TRIGGER `add_subscribed_users`
AFTER INSERT ON `website_queries`
FOR EACH ROW
INSERT INTO users (firstName, lastName, email, createdAt, subscribed)
SELECT NEW.firstName, NEW.lastName, NEW.email, NEW.createdAt, NEW.subscribed
WHERE NEW.subscribed = 0;
In this single-statement form you do not need in BEGIN-END and DELIMITER too.

MySQL Stored Procedure Case Statement Syntax Error - Contd 2

One Syntax Error in the following code.
The response of the mysql was
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 ''300ml','500ml','1lit','2lit')
VALUES(m_cust_id,IN_book_id,(SELECT Rate.Can*Book' at line 24 SQL
Statement
The following is the procedure.
CREATE PROCEDURE `calculate_amount` (in IN_book_id INT, in IN_qty INT )
BEGIN
-- declare
DECLARE m_prdct VARCHAR(10);
DECLARE m_cust_id INT(5);
-- select into
SELECT
Product
FROM
Bookings
WHERE
Book_id = IN_book_id INTO m_prdct;
SELECT
Cust_id
FROM
Bookings
WHERE
Book_id = IN_book_id INTO m_cust_id;
-- conditionals & action
IF (m_prdct = '20ltr') THEN
INSERT INTO Amount (Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,(SELECT Rate.Can*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0,0);
ELSEIF (m_prdct = '300ml') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,(SELECT Rate.300ml*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0);
ELSEIF (m_prdct = '500ml') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,(SELECT Rate.500ml*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0);
ELSEIF (m_prdct = '1ltr') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,0,(SELECT Rate.1lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0);
ELSE
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,0,0,(SELECT Rate.2lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id));
-- end
END IF;
END
The script applied successfully even if it showed error. The Back Tick to column names starting with digits solved the issue. Upon Refreshing, it got changed from _SYNTAX_ERROR to its actual name.

mysql different row updates depending on a variable (stored procedure)

CREATE PROCEDURE update_table(
IN choice INT(4),
IN id VARCHAR(50),
IN string VARCHAR(50)
)
BEGIN
UPDATE salesman
set salesman_name = IF(choice = 1, string, salesman_name)
where salesman_id = id
UPDATE salesman
set date = IF(choice = 2, string, date)
where salesman_id = id
END
if choiceis 1, change salesman_name as string
if choice is 2, change date as string
can you explain me what i'm doing wrong?
it works fine with a single update, my guess is there is another way to implement if but i couldn't.
if choice = 1 then
update salesman set salesman_name = string where salesman_id = id
...i tried this version too but still, not working.
DELIMITER //
CREATE PROCEDURE update_table(
IN choice INT(4),
IN id VARCHAR(50),
IN string VARCHAR(50)
)
BEGIN
UPDATE salesman set salesman_name = IF(choice = 1, string, salesman_name) where salesman_id = id;
UPDATE salesman set date = IF(choice = 2, string, date) where salesman_id = id;
END //
DELIMITER ;
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
also says this:
ERROR: Unknown Punctuation String # 11 (last line)
When a stored procedure has more than one statement, they need to be terminated with ;
To do that, you need to tempoararily change the delimiter so you can end the procedure. Here's a SO answer with an example of how to do that: MySQL create stored procedure syntax with delimiter

stored procedure with count(*) and if statement

I have a problem with this sql code.
I have a table friends with three columns user1, user2, pending.
user1 and user2 are primary keys of datatype int.
The phpmyadmin returns this error:
MySQL said: #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 'Declare var int; Set var = SELECT COUNT(*) FROM friends WHERE ((user1 = id1 A' at line 1
note - i did it via phphmyadmin so i have
This is my code:
Declare var int;
Set var = SELECT COUNT(*) FROM friends WHERE ((user1 = id1 AND user2=id2) OR (user1 = id2 AND user2=id1));
IF var = 0
BEGIN
INSERT INTO friends
( user1, user2,pending)
VALUES (id1, id2,1);
Print 'Data now added.';
END
ELSE
BEGIN
Print 'Dah! already exists';
END
Try to invert those 2 lines :
...
declare #var int
AS
...
As follow :
...
AS
declare #var int
...
1) Everything (including declarations) must be in a BEGIN ... END block.
2) You don't need to (and cannot) declare #session_veriables, only local_variables.
3) Missing parameters. They must be declared between parantheses: proc_name(p1 INT, p2 INT)
4) All statements must terminate with ;
5) The whole procedure should be wrapped inside:
DELIMITER ||
...
||
DELIMITER ;
unless you send it via phpMyAdmin.

MySql Error 1054 when inserting into table that has trigger

I have created a trigger on a table, and when I try to insert data into it I get MySql error "Error Code: 1054 'unknown column license_key in field list'"
The INSERT I try to do is this:
INSERT INTO LOAD_MASTER(license_key, state, total_pageviews, total_visitors, max_visitors, max_pageviews, ip, secret, read_time, url)
VALUES ("order55555hytgtrfderfredfredfredftyu8ikloi98nhygt6", "preparing", 400, 1000, 200, 400, "202,2,35,36", "Hemmeligheden", 120, "http://google.dk");
The Trigger I have created is supposed to check the TABLE LOAD_STATS for the presence of the IP entered in the LOAD_MASTER table, and then based on the results change a value in another table called LICENSE
My trigger is here:
DELIMITER //
CREATE TRIGGER ip_check AFTER INSERT ON LOAD_MASTER FOR EACH ROW
BEGIN
DECLARE MK varchar(50);
DECLARE MIP varchar(15);
DECLARE LID int(6);
SET MK = license_key;
SET MIP = ip;
SET LID = (
SELECT l.license_id
FROM license_keys k, license l
WHERE l.license_id = k.license_id
AND k.license_key = MK
);
IF (SELECT COUNT(DISTICT(master_ip)) FROM LOAD_STATS WHERE master_ip = MIP AND master_key = MK ) > 3 THEN
UPDATE LICENSE
SET STATE = 0
WHERE license_id = LID;
END IF;
END
//
DELIMITER ;
Any help on why I get this error would be much appriciated.
-Dan
In these lines
SET MK = license_key;
SET MIP = ip;
you probably meant to address NEW.license_key and NEW.ip respectively.
IMHO you can make more succinct. Try this
DELIMITER //
CREATE TRIGGER ip_check
AFTER INSERT ON load_master
FOR EACH ROW
BEGIN
IF (SELECT COUNT(DISTICT(master_ip))
FROM load_stats
WHERE master_ip = NEW.ip
AND master_key = NEW.license_key ) > 3 THEN
UPDATE license
SET state = 0
WHERE license_id =
(
SELECT l.license_id
FROM license_keys k JOIN license l
ON l.license_id = k.license_id
AND k.license_key = NEW.license_key
); -- make sure that this subquery returns only ONE record
END IF;
END //
DELIMITER ;
There is some room for improvement rewriting an UPDATE with JOIN instead of using a subquery