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 0, 30'
I get this error when I try to use this code :
DROP TABLE IF EXISTS prodaja;
CREATE TABLE prodaja ( id SERIAL, id_stvari BIGINT, id_kupca BIGINT, kvantitet INT, cijena DECIMAL(9,2), podmireno INT );
INSERT INTO prodaja (id_stvari, id_kupca, kvantitet, cijena, podmireno) VALUES (1, 3, 5, 19.95, 0);
INSERT INTO prodaja (id_stvari, id_kupca, kvantitet, cijena, podmireno) VALUES (2, 2, 3, 14.95, 1);
INSERT INTO prodaja (id_stvari, id_kupca, kvantitet, cijena, podmireno) VALUES (3, 1, 1, 29.95, 0);
SELECT * FROM prodaja;
DELIMITER //
CREATE TRIGGER updateprodaja BEFORE UPDATE ON prodaja
FOR EACH ROW
BEGIN
IF ( SELECT podmireno FROM prodaja WHERE id = NEW.id ) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: Ne mogu azurirati red imena podmireno u tablici prodaja';
END IF;
END
//
DELIMITER ;
START TRANSACTION;
UPDATE prodaja SET kvantitet = kvantitet + 9 WHERE id = 1;
COMMIT;
I really don't know where the problem is... Can someone help me out?
You are using a UI that automatically tacks on LIMIT 30 to whatever you enter? What version of what UI? Also, what version of MySQL?
Solution: Upgrade the UI.
Related
I'm trying to create a trigger that will update some value, and if the row is not exists than insert a new row with value = 1.
I wrote the trigger in 3 possible ways and all of them are getting error
version 1:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics
SET item_id = NEW.item_id,
likes = 0,
comment = 1
ON DUPLICATE KEY UPDATE
SET comment = OLD.comment + 1;
END;
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 comment = OLD.comment + 1' at line 9
version 2:
CREATE TRIGGER stat_trg AFTER INSERT ON comments
FOR EACH ROW
BEGIN
DECLARE num integer DEFAULT 0;
SELECT comment into num FROM statistics
WHERE item_id = OLD.item_id;
SET num := num + 1;
INSERT INTO statistics (item_id, comment, likes) VALUES (num,1,0)
END;
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 4
version 3:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics (item_id, likes, COMMENT) VALUES (NEW.item_id,0,1)
ON DUPLICATE KEY UPDATE
comment = comment + 1;
END;
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 6
All looks good according to examples that I found, what am I missing?
Please help on this issue.
Thanks.
Please try this:
DELIMITER $$
CREATE TRIGGER `stat_trg` BEFORE INSERT ON `comments`
FOR EACH ROW BEGIN
INSERT INTO `statistics` set item_id= NEW.item_id, likes = '0', comments = '1' ON DUPLICATE KEY UPDATE
comments = values(comments) + 1;
END$$
DELIMITER ;
here is mycode
i am trying to create a trigger after insert on a table say product table to a change traker table called audit table
like this
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
)
END
//
DELIMITER ;
this is the error i am getting
i tried all these ways
used | ,S etc instead of // and
removed ; after end, placed // or \ or $$ together and below end
nothing works, some one please help me
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' at line 32
some one please help me
Place sql terminator ";" after create table statement
remove ";" after end delimeter.
So your code should be as per below,
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
);
END
//
DELIMITER
The solution provided by #juergen in comments is working fine, that is, Add a ; after the insert statement (before the END) Thanks for finding my mistake. I was looking for it for about 4 hours so Answer is here:
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table
FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number
into #l_product_description, #l_product_number
from product_table p
where p.product_description =
(select max(pg.product_number)
from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
); //<<---- Semicolon needed to be here
END
//
DELIMITER;
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);
What is wrong with the syntax or query?
SET #q=1000;
SET #p=5.00;
SELECT #ccount := COUNT(*) FROM 1detail WHERE price>=#p;
if(#ccount='0') THEN
INSERT INTO 1detail (price,quantity) VALUES (#p,#q);
ELSE
INSERT INTO 2detail (price,quantity) VALUES (#p,#q);
END IF;
It gives me 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(#ccount='0') THEN
INSERT INTO 1detail (price,quantity) VALUES (#p,#q)' at line 1:
To create table (that is working):
CREATE TABLE 1detail
(
quantity int,
price DECIMAL(15,2)
);
CREATE TABLE 2detail
(
quantity int,
price DECIMAL(15,2)
);
INSERT INTO 1detail
(quantity, price)
VALUES
('5', '10');
Table names that start with digits need to be escaped. And, if is only used inside functions, stored procedures, and triggers. Assuming this code is "if"-safe:
SET #q=1000;
SET #p=5.00;
SELECT #ccount := COUNT(*) FROM `1detail` WHERE price>=#p;
if(#ccount='0') THEN
INSERT INTO `1detail`(price,quantity) VALUES (#p,#q);
ELSE
INSERT INTO `2detail`(price,quantity) VALUES (#p,#q);
END IF;
If not, you can just do this as two inserts:
INSERT INTO `2detail`(price,quantity)
VALUES (#p, #q)
WHERE exists (select 1 from `detail` where price >= #p)
INSERT INTO `1detail`(price,quantity)
VALUES (#p, #q)
WHERE not exists (select 1 from `detail` where price >= #p)
check the manual that corresponds to your MySQL server version for the right syntax to use near: 'SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed' at line 2
my code:
DROP TRIGGER IF EXISTS newsmonitoringrawdata.TNTInsertionTrigger;
CREATE TRIGGER TgtInsertionTrigger BEFORE INSERT ON tnews
FOR EACH ROW BEGIN
SET #isRecordCreated =
SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed=NEW.Feed
AND
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i');
IF #isRecordCreated>0 THEN
UPDATE capacity
SET MsgNumber=MsgNumber+1,
size=size+NEW.MSize
WHERE
server=NEW.server
AND
feed=NEW.feed
and
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i');
ELSE
INSERT INTO capacity(server, feed, streamdt, msgNumber, size)
VALUES
(
NEW.server,
NEW.feed,
date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i:00'),
1,
NEW.size
);
END IF;
END
SET #isRecordCreated = (
SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed=NEW.Feed
AND
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i')
);
You have to add parantheses () around the query.